Access VBA Classes Arrays - class

Im trying to use some classes that will hold invoice data and I cant seem to get it working. I just want it to save details of an invoice in an array. Heres the code, maybe someone can figure it out.
Also id like for the LineItems to take a random number of product items, not sure how to do that yet. Im doing this to eventually send the data through a web service from ms access.
Here is the other code for struct_XOrder:
Public OrderNumber As String
Public ClientShortName As String
'"LineItems" is an array with elements defined as struct_LineItem
'See Complex Types: Arrays in Microsoft Office 2003 Web Services Toolkit Help
'for details on implementing arrays.
Public LineItems As Variant
Public OrderError As String
Heres the struct_LineItem:
Public ProductSKU As String
Public Qty As Long
Public UnitPrice As Variant
Public ItemComment As String
Public ItemError As String
Heres my main code
Sub webservicetest()
Dim NewOrder As struct_XOrder
Dim LineItems(1 To 2) As Variant
Dim Xline(1 To 2) As Variant
Set Xline() = struct_LineItem
Set NewOrder = New struct_XOrder
Set NewOrder.LineItems() = New struct_LineItem
Set NewOrder.LineItems() = New Xline
'Xline = New struct_LineItem
Xline(1).ItemComment = "items"
Xline(1).Qty = 10
NewOrder.LineItems() = Xline()
NewOrder.ClientShortName = "DemoClient"
NewOrder.OrderNumber = "12345"
'Xline(1).ItemComment = "item1"
'Xline(1).Qty = 5
Debug.Print NewOrder.ClientShortName
Debug.Print NewOrder.OrderNumber
'For i = LBound(Xline) To UBound(Xline): Debug.Print Xline(i): Next
'Debug.Print Xline(1).ItemComment
'Debug.Print Xline(1).Qty
End Sub

See comments for explanations.
Use Type instead within VBA to create structs. Also, you do not need to use Set the way you are.
Additionally, use Option Explicit to require variable declarations whenever using VBA unless you want a nightmare.
This all can go in the same module.
'use this
Option Explicit
'VBA structs are defined like the following
'and do not need "new" (similar to other languages)
'when creating them
Type struct_LineItem
ProductSKU As String
Qty As Long
UnitPrice As Variant
ItemComment As String
ItemError As String
End Type
Type struct_XOrder
OrderNumber As String
ClientShortName As String
'make this more clear
LineItems() As struct_LineItem
OrderError As String
End Type
Sub webservicetest()
Dim NewOrder As struct_XOrder
Dim LineItems(1 To 2) As Variant
Dim Xline(1 To 2) As struct_LineItem
'you can't do this, you need to specify which element
'in XLine you want to set
'Set Xline() = struct_LineItem
Dim myStruct As struct_LineItem
Xline(1) = myStruct
'you don't need "set"
NewOrder.LineItems = Xline()
'this won't update as if it's a reference, btw
Xline(1).ItemComment = "items"
Xline(1).Qty = 10
NewOrder.LineItems = Xline()
NewOrder.ClientShortName = "DemoClient"
NewOrder.OrderNumber = "12345"
Debug.Print NewOrder.ClientShortName
Debug.Print NewOrder.OrderNumber
'don't do ":" as this makes code unbearably not readable...
'For i = LBound(Xline) To UBound(Xline): Debug.Print Xline(i): Next
Dim i As Integer
For i = 1 To UBound(NewOrder.LineItems)
Debug.Print NewOrder.LineItems(i).ItemComment
Next i
End Sub

Related

macro executed when document is loaded

After losing files from a HD, data rescue gave me back most of them, but with abstract names (such as file000123.xlsx). I need to rename them using cell values (client name, invoice ref).
I could make a Basic macro that works for this, if I open the files and start the macro myself for each one of those files.
As I have thousands of files to rename, I need that macro to execute on its own, either when documents are loaded, otherwise on a selected folder.
I assigned my macro to the "document loaded" event via the "tools-Customize-Events" menu. Then, I get a "wrong property value" error on the 1st line, the one defining the function.
Is my way of doing wrong ? Do I have to modify the macro for it to work there ?
Context :
This macro is in "My macros", not within the documents.
using libreOffice 7 on Linux
working on .xlsx files
Thanks for any help, my libreOffice Basic is even poorer than my English.. B-)
My code :
function getFullRep(sPath As String) As String
Dim cpt As Integer
Dim buf As String
Const SLASH = "/"
buf = ""
for cpt = Len(sPath) to 1 step -1
if Mid(sPath, cpt, 1) = SLASH then
buf = Left(sPath, cpt)
exit for
end if
next
getFullRep = buf
end function
Sub Main
Dim oDoc as Object
Dim sG2 as String
Dim sO2 as String
Dim sO as String
Dim sPathBackupFolder as String
Dim filespec As string
Dim laDate As String
Dim myfilename As String
Dim oFeuille As Object
Dim sNomFeuille As String
oDoc = ThisComponent
sPathBackupFolder = getFullRep (oDoc.location)
oFeuille = oDoc.getCurrentController().getActiveSheet()
sNomFeuille = oFeuille.getName()
laDate = Format(Now(),"YYMMDDhhmmss")
If oFeuille.GetCellRangeByName("G14").String <>"" Then
sG2 = oFeuille.GetCellRangeByName("G14").String
sO2 = oFeuille.GetCellRangeByName("A15").String
else
sG2 = oFeuille.GetCellRangeByName("G15").String
sO2 = oFeuille.GetCellRangeByName("A16").String
End If
sO = Replace(sO2, "Facture N°", "")
'Chemin et nom de fichier composé
myfilename = sPathBackupFolder+sG2+" - "+sO+" - "+laDate+".ods"
'Enregistrer sous
dim document as object
dim dispatcher as object
' ----------------------------------------------------------------------
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
' ----------------------------------------------------------------------
dim args1(1) as new com.sun.star.beans.PropertyValue
args1(0).Name = "URL"
args1(0).Value = ConvertToUrl(myfilename) 'On converti le chemin
args1(1).Name = "FilterName"
args1(1).Value = "calc8"
dispatcher.executeDispatch(document, ".uno:SaveAs", "", 0, args1())
oDoc.store
oDoc.close(True)
End Sub
You are getting the error because the document has not completely opened yet. Instead, attach to the event "View Created", and make sure to save to "LibreOffice" (as opposed to individual document).
By the way, it would be a good idea to give your sub a more appropriate name than "Main".
Hope that helps!

Lotusscripts Get rich text field and send Email

I try to coding is sending Rich text field via email , but I find an error that's I think this method for sending email, by following code
Sub Click(Source As Button)
Dim s As New NotesSession
Dim w As New NotesUIWorkspace
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim uidoc As NotesUIDocument
Set uidoc = w.CurrentDocument
Set s = New NotesSession
Set w = New NotesUIWorkspace
Set db = s.CurrentDatabase
Set doc = New NotesDocument (db)
doc.sendTo =s.UserName
doc.Subject = "Employee Information"
Dim rt As NotesRichTextItem
Set rt = New NotesRichTextItem ( doc, "Body" )
'Dim file As Variant 'if I use this code for declare for get value; Error : Type Mismatch
'Set file = doc.GetFirstItem("Body")
Dim rtitem As NotesRichTextItem 'if I use this code for declare for get value ; Error : Missing text object
Set rtitem = doc.GetFirstItem( "Body" )
Call rt.AppendRTItem(rtitem)
doc.Send(False)
End Sub
One thing I notice is that you don't set the form on the mail document you are creating.
You have some code you don't need, since you don't use uidoc anywhere, no need for that or declaring a NotesUIWorkspace object.
I also recommend that you use better variable names, and not to use extended notation when you set field values in a NotesDocument object.
I suggest that you take a look at the articles here:
http://blog.texasswede.com/how-to-write-better-code-in-notesdomino/
Below is the code that I cleaned up:
Option Public
Option Declare
Sub Click(Source As Button)
Dim session As New NotesSession
Dim ws As New NotesUIWorkspace
Dim db As NotesDatabase
Dim mailDoc As NotesDocument
Dim mailBody As NotesRichTextItem
Set db = session.CurrentDatabase
Set mailDoc = New NotesDocument(db)
Call mailDoc.ReplaceItemValue("Form","Memo")
Call mailDoc.ReplaceItemValue("SendTo",session.UserName)
Call mailDoc.ReplaceItemValue("Subject","Employee Information")
Set mailBody = New NotesRichTextItem(mailDoc,"Body" )
'Dim file As Variant 'if I use this code for declare for get value; Error : Type Mismatch
'Set file = doc.GetFirstItem("Body")
Dim rtitem As NotesRichTextItem 'if I use this code for declare for get value ; Error : Missing text object
Set rtitem = doc.GetFirstItem("Body")
Call mailBody.AppendRTItem(rtitem)
Call mailDoc.Send(False)
End Sub
The big question here is where you are getting the rich text field you want to send from? In your original code you are trying to read it from the newly created document (the one I call mailDoc). But that does not make any sense.
Your problem is simply that you are not reading the rich text from anywhere.
If your goal is to send an email, you can use my mail notification class:
http://blog.texasswede.com/updated-mailnotification-class-now-with-html-email-support-and-web-links/
Then your code would look something like this:
Dim session As New NotesSession
Dim mail As NotesMail
' *** Create a mail
Set mail = New NotesMail()
' Set receipient and subject
mail.MailTo = session.CommonUsername
mail.Subject = "Employee Information"
mail.Principal = "noreply#example.com"
' Create body content from rtitem.
' Yes, I should have added a method in the
' class to append RichtText to the mail body...
mail.body.AppendRTItem(rtitem)
Call mail.Send()
The only thing you have to do is to get the rtitem from somewhere. Since your original code declared a NotesUIWorkSpace object and a NotesUIDocument object, I am guessing you want to read it from the currently open document. Then you just add the following to the beginning of the code:
Dim ws As New NotesUIWorkspace
Dim thisdoc As NotesDocument
Dim rtitem as NotesRichTextItem
Set thisdoc = ws.CurrentDocument.Document
Set rtitem = thisdoc.GetFirstItem("Body")
Do you also see how much easier it is to read when you use descriptive variable names?
Hi you did not saved the document. Please be aware the Richtext is not available if the document is not saved.

Use current form name in expression builder to create a query

I would like to refer to my current form name in the expression builder in order to create a query.
I have created the following function:
Public Function FormAtual()
Dim frm As Form
Set frm = Screen.ActiveForm
End Function
And then used it in the Expression Builder like that:
[Formulários]![FormAtual]![Lista0]
But it does not work.
Not quite sure what you are after, but to obtain the name of the active form:
Public Function FormAtual() As String
FormAtual = Screen.ActiveForm.Name
End Function
Then, to have an expression:
[Formulários](FormAtual)![Lista0]
Below is something that works with a form I am using and the steps I took. This code can be simplified, but I wanted to show how the pieces fit together.
First, create the following in a Global Module
Public Function GetVariableValue(ctlName As String) As String
Dim sFrmName As String
Dim sVal As String
' First get the Active Form Name
sFrmName = Screen.ActiveForm.Name
' Now get the value of the desired control
sVal = Forms(sFrmName).Controls(ctlName)
' Display the Form/Ctl/Value
MsgBox "Form Name: " & sFrmName & vbTab & "CtlName: " & ctlName & vbTab & "Ctl Value: " & sVal
End Function
Public Function GetFormName() As String
GetFormName = Screen.ActiveForm.Name
End Function
Next, use the query builder to select the Function and then specify the desired control. i.e.
SELECT GetVariableValue('MyID') AS Expr1, [0_Stack].Resolved, [0_Stack].WebAddress, [0_Stack].DateAdded, [0_Stack].DateChanged, [0_Stack].MyID
FROM 0_Stack
WHERE ((([0_Stack].MyID)=GetVariableValue('MyID')));

How to email multiple doclinks of those who are my direct reports in the view?

I have a question. For example there is one view and 10 documents are in that view. Out of all those documents, 8 of them I should be the recipient of the email (based on the field value which is my email address).
Now, what I want to happen is that I will be receiving only one email for all those 8 documents, and in that email I there will be 8 doclinks.
Is that possible?
'Cause currently I am getting 8 emails and for each email, there is one doclink. Thanks in advance for those who can help me.
Dim s As NotesSession
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim rtitem As NotesRichTextItem
Dim i As Integer
Dim view As NotesView
Set s = New NotesSession
Set db = s.CurrentDatabase
Set view = db.GetView("View")
Set doc = New NotesDocument(db)
Dim addresses As NotesName
i=0
Set doc = view.GetFirstDocument
While Not(doc Is Nothing)
Set addresses = New NotesName(doc.Manager(0))
If addresses.abbreviated = "" Then
i = i + 1
Else
doc.SendTo = addresses.abbreviated
doc.Form = "Memo"
Set rtitem = New NotesRichTextItem(doc, "Body")
Call rtitem.AppendText("Balance")
Call rtitem.appenddoclink(doc, "Link")
doc.Send (True)
i = i + 1
End If
Set doc = view.GetNextDocument(doc)
Wend
As Thorsten said, it can be done. There are a couple of ways to handle this, depending on how flexible and future proof you want it, and how "clean" of a solution you want.
Let's say you have 10 documents, 8 with your email and 2 with a different user's email. I assume you want to send one mail to you with 8 doc links and one to the other person with 2 doc links.
The way I would do it is to create a class. That class would contain a list of NotesDocuments (and a method to add documents to the list):
Class DocData
Public docs List As NotesDocument
Public Sub New()
End Sub
Public Sub Add(doc as NotesDocument)
Set docs(doc.UniversalID) = doc
End Sub
End Class
In your main code, you have a list of DocData objects, one per mail recipient:
Dim docs List As DocData
You now loop through the view or the document collection you have. You check the email address for each document and if there isn't a list item for that address, you create it and add the document to it. If it already exists, just add the document:
email = doc.GetItemValue("EmailAddress")(0)
If !IsElement(docs(email)) Then
Set docs(email) = New DocData()
End If
Call docs(email).Add(doc)
When all documents have been processed, you should have a list with one item per mail recipient, and you can loop through the list, build one email per item and populate it with doc links for all the documents in the list in the object.
For performance reasons, if you plan to have this working on larger views with more documents, I woudl suggest that you put the email address in one of the columns, and then use view entries to loop though the documents, and ColumnValues() to read the email address.
I wrote about it here: http://blog.texasswede.com/re-using-lotusscript-lists-to-increase-performance/
Your code can be slightly modified to only send one mail:
Dim s As NotesSession
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim rtitem As NotesRichTextItem
Dim i As Integer
Dim view As NotesView
Set s = New NotesSession
Set db = s.CurrentDatabase
Set view = db.GetView("View")
Set doc = New NotesDocument(db)
Dim addresses As NotesName
i=0
'- prepare mail
doc.Form = "Memo"
Set rtitem = New NotesRichTextItem(doc, "Body")
Call rtitem.AppendText("Balance")
Set doc = view.GetFirstDocument
While Not(doc Is Nothing)
Set addresses = New NotesName(doc.Manager(0))
If addresses.abbreviated = "" Then
i = i + 1
Else
'- Set recipient
If not doc.HasItem( "SendTo" ) then
doc.SendTo = addresses.abbreviated
End If
'- Append descriptive text, link and new line
Call rtitem.appendtext(doc.Subject(0) & " " )
Call rtitem.appenddoclink(doc, "Link")
Call rtitem.addnewline(1)
i = i + 1
End If
Set doc = view.GetNextDocument(doc)
Wend
'- send mail
Call doc.Send (True)
with that code the mail is first prepared, then a doclink is added for every document, and in the end the mail is sent.

Access VBA visible control form from another form

I have a table and form setup to control another form in my database.
I'm wanting to make a code that will take the title from my field and add it to my code as a variable to change the visibility options of my other form.
my form is set with all the of the names to all objects on the form I want to control.
LSE_FORM_ADMIN = The table with all the LSE_FORM_ALL names in it.
Table is setup with 3 columns key, names and a checkbox which I put into a form to make a continuous list.
here is my code on the form, but I keep getting and runtime 424: object required error:
Private Sub Form_Current()
Dim VARSET As Object
Dim VAR As String
VARSET = DLookup("TITLE", Table!LSE_FORM_ADMIN, "") 'keep getting error here
VAR = VARSET
If Me!CB = "-1" Then
Form_LSE_FORM_ALL!VAR.Visible = True
Else
Form_LSE_FORM_ALL!VAR.Visible = False
End If
End Sub
can someone help me fix this code so that it will grab the title field data and make it a variable to add to the rest of the code?
It's difficult to see exactly what you are trying to achieve, but your problems stem from using the variant variable type when you should be using an explicit Form or Control type. Using your last example.
RSTT.Visible = True 'getting Run-time error '424': object required
This is because you have declared RSTT as a variant. The line
RSTT = "Form_LSE_FORM_ALL" & "!" & (RST)
results in the variable RSTT containing a string, which does not have a property ".Visible"
Set DB = CurrentDb
Set RS = DB.OpenRecordset("LSE_FORM_ADMIN")
These lines are redundant as you have the values that you need available on the form fields which are already bound to the table LSE_FORM_ADMIN.
As far as I understand, you have a continuous form (ADMIN?) bound to the table LSE_FORM_Admin. As you step through the records on this form, you want code to be fired which takes the value of the TITLE field/control and use it to set a control with the same name, on a separate form, Form_LSE_FORM_ALL, to be (in)visible, dependent on the value of the checkbox control name CB on the ADMIN form?
If you want the ADMIN form to make the changes "live" to the ALL form, you should consider using an event of the CB checkbox control. Using the current event of the form means that the changes you make will not be reflected in the ALL form until you step out of the record you have just edited, then back in, to fire the form's Current event on that record.
Example using AfterUpdate event of CB checkbox
Private Sub CB_AfterUpdate()
Dim strRST As String
Dim frmTarget as Form
Dim ctlRSTT As Control
Set strRST = Me!TITLE
Set frmTarget = Forms("Form_LSE_FORM_ALL")
Set ctlRSTT = frmTarget.Controls(strRST)
ctlRSTT.Visible = Me!CB 'getting Run-time error '424': object required
End Sub
really not sure how to do the syntax when doing a recordset to a table from the form, need some help with that.
here is my code and attempt at the record set:
Private Sub Form_Current()
Dim DB As Database
Dim RS As Recordset
Dim RST As String
Set DB = CurrentDb
Set RS = DB.OpenRecordset("LSE_FORM_ADMIN")
Set RST = RS 'GETTING OBJECT REQUIRED ERROR ON "RST ="
Do Until RS.EOF
RST = Me!TITLE
RS.MoveNext
Loop
If Me!CB = "-1" Then
Form_LSE_FORM_ALL!RS.Visible = True
Else
Form_LSE_FORM_ALL!RS.Visible = False
End If
End Sub
I think I know what you are trying to do, but your descriptions / references are not matching up. Please look at the following comments and clarify:
1. You say "...make a code that will take the title from my field and ..." but your code is taking "Me.Title", "ME" is a reference to the Form - not a field.
2. Your code is in the "Form_Current" event, which means it will fire for every record you process. That will work, but I think you want to do this code only once to be more efficient.
3. You have no provision for processing more than one field. I think you need to loop through all fields in your table, setting visible to true or false.
The following is my suggestion, but I will update once you clarify the issues.
Option Compare Database
Option Explicit
Dim DB As DAO.Database
Dim RS As DAO.Recordset
'Dim RST As Variant
'Dim RSTT As Variant
Public Sub FORM_CURRENT()
Set DB = CurrentDb
Set RS = DB.OpenRecordset("LSE_FORM_ADMIN")
Do While Not RS.EOF ' Loop thru all field names for the form
If RS!HideYN = True Then ' Desire to hide the field?
Me(RS!ctlname).Visible = False ' Yes, hide the field.
Else
Me(RS!ctlname).Visible = True ' No, show the field
End If
RS.MoveNext ' Get next field name
Loop
RS.Close
Set RS = Nothing
Set DB = Nothing
'Set RST = Me!Title
'RSTT = "Form_LSE_FORM_ALL" & "!" & (RST)
'If Me!CB = "-1" Then
' RSTT.Visible = True 'getting Run-time error '424': object required
'Else
' RSTT.Visible = False
'End If
End Sub
Final code, thanks to Cheesenbranston.
Private Sub Form_AfterUpdate()
Dim strRST As String
Dim frmTarget As Form
Dim ctlRSTT As Control
strRST = Me!TITLE
Set frmTarget = Forms("LSE_FORM_ALL")
Set ctlRSTT = frmTarget.Controls(strRST)
If Me!CB = "-1" Then
ctlRSTT.Visible = True
Else
ctlRSTT.Visible = False
End If
End Sub
#Cheesenbranston: Your original code was more like a toggle of on and off so if my object was not visible then my trigger checkbox would make it visible when checked, more of a quality of life for my own needs, none the less worked. Also strRST doesn't need SET since its just a String. Thanks again =D very happy day!