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')));
Related
Please disregard, just found an answer to this:
In my MsAccess form I currently have:
Keywords window - where I can type text to look up records, by the Client Name,
Search button - after pressing on it I can see the search results
Show All button - after pressing on which I see all the records
Currently, after pressing Show All, whatever I typed in the Keywords window stays there. I wish that the keywords would be erased, every time I press Show All button
Is it possible to have such result?
If yes, what do I do or - how can I modify my Event Procedure?
Below are my event procedures codes:
btn_Search_Click() - to search by the ceratin keywords
btn_ShowAll_Click() - to show all records
Option Compare Database
Option Explicit
Private Sub btn_Search_Click()
Dim strsearch As String
Dim strText As String
strText = Me.TxtKeywords.Value
strsearch = "SELECT * from qry_Clients
where ((ClientName LIKE ""*" & strText & "*"") OR(MedRecNumber LIKE ""*" &
strText & "*""))"
Me.RecordSource = strsearch
End Sub
Private Sub btn_ShowAll_Click()
Dim strsearch As String
strsearch = "SELECT * from qry_Clients"
Me.RecordSource = strsearch
End Sub
Thank you!
Sorry, but I just found an answer!
In order to be able to clear the textbox after hitting "Show All" button,
I have to add the last string into my
Private Sub btn_ShowAll_Click():
Me.TxtKeywords = ""
I have this code set in access, but no email is sending upon clicking the button on the form. I have outlook open. When i click the button on the form, i can't see anything that actually happens. I want the email address to be equal to the value in [text1], and I am trying to make the subject include a fixed message plus the input from [text2]. Even without these variables, I can't get this to work
Public Sub Command495_Click()
Dim mailto As String
Dim ccto As String
Dim bccto As String
mailto = [text1]
ccto = ""
bccto = ""
emailmsg = "trial"
mailsub = [text2] & ", Does this work?"
On Error Resume Next
DoCmd.SendObject acSendNoObjectType, , acFormattxt, mailto, ccto, bccto, mailsubj, emailmsg, True
End Sub
I have checked to make sure the onclick property shows event procedure. I am stuck, please help!
Here are a few suggestions and a modified version of your code.
ALWAYS use Option Explicit and compile your module before testing. You had a number of variables that were not defined and incorrect spelling of some options.
NEVER bypass errors when testing (get rid of your "On Error Resume Next") That's why you never saw an error.
Look for every place I entered ">>>" and address that issue.
Always explicitly define your variables and use the proper Type. Removes all doubt of what/where something is.
Option Compare Database
Option Explicit
Public Sub Command495_Click()
Dim mailto As String
Dim ccto As String
Dim bccto As String
Dim emailmsg As String
Dim mailsub As String
mailto = [Text1] ' >>> Where is [Text1]?? Remove for testing
ccto = ""
bccto = ""
emailmsg = "trial"
mailsub = [Text2] & ", Does this work?" ' >>> Where is [Text2]?? Remove for testing
' >>> Bad idea to ignore errors when testing!!
On Error Resume Next
'>>> Following line had: (1) 'acSendNoObjectType' which is incorrect; (2) mailsubj, which is undefined
DoCmd.SendObject acSendNoObject, , acFormatTXT, mailto, ccto, bccto, mailsub, emailmsg, True
End Sub
I've spent two days on this and I'm still not able to figure it out 8-)
I have a LibreOffice Writer document with some Placeholders (Insert -> Fields -> More Fields -> Functions -> Placeholder -> Image) and Input fields (Insert -> Fields -> More Fieds -> Functions -> Input field) and I need to retrieve the value of an Input field and use it to replace a specified Placeholder in the same document.
To be more precise. I have an Input field where I enter for example 123
and somewhere in the document is a button, which triggers a macro, and this macro should:
retrieve the current value of the specified (named?) Input field ("123"),
"replace" a specified (named?) Placeholder with an image loaded from http://domain.tld/image/123.png
Is this somehow possible? Would be great, because I'm trying to to insert externally generated barcodes into my document...
These are both "Text fields", and some information and macro examples are in Andrew Pitonyak's book OpenOffice Macros Explained (available as a free pdf download from http://www.pitonyak.org/oo.php). The wiki page also has some good background.
Form controls (from the toolbar "Form controls") are named, so they have an advantage when working with macros. Text fields, however - the kind you have in your document - are not named, so you have to cycle through all the fields in a document, or highlight a particular run of text and cycle through the field within the highlighted area to find the one you are after. The Pitonyak document has examples of both methods.
Assuming the document has only one input field, this StarBasic code will print its current value:
Sub DisplayFields
Dim oEnum As Object
Dim oField As Object
oEnum = ThisComponent.getTextFields().createEnumeration()
Do While oEnum.hasMoreElements()
oField = oEnum.nextElement()
If oField.getPresentation(True) = "Input field" Then
Print "Input field contents: " & oField.getPresentation(False)
Exit Do
End If
Loop
End Sub
As far as I can tell, there is no API to replace a placeholder with its designated content. There might be a way with the dispatcher - the list of dispatch commands tantalizingly includes "FieldDialog" - but I wasn't able to find any documentation or examples.
I think what you'd have to do is find the field, put your cursor there, insert the image, then delete the placeholder field. Some more StarBasic code (again, assuming there's only a single placeholder field in the document):
Sub InsertImage
Dim oEnum As Object
Dim oField As Object
Dim oAnchor As Object
Dim oText As Object
Dim oCursor As Object
Dim FileName As String
Dim FileURL As String
Dim objTextGraphicObject As Object
oEnum = ThisComponent.getTextFields().createEnumeration()
Do While oEnum.hasMoreElements()
oField = oEnum.nextElement()
If oField.getPresentation(True) = "Placeholder" Then
oAnchor = oField.Anchor
oText = oAnchor.getText()
oCursor = oText.createTextCursorByRange(oAnchor.getEnd)
FileName = "C:\after zoo.JPG"
FileURL = convertToURL(FileName)
objTextGraphicObject = ThisComponent.createInstance("com.sun.star.text.TextGraphicObject")
REM Optional to set the size
' Dim objSize as New com.sun.star.awt.Size
' objSize.Width = 3530
' objSize.Height = 1550
' objTextGraphicObject.setSize(objSize)
objTextGraphicObject.GraphicURL = FileURL
oText.insertTextContent(oCursor.Start, objTextGraphicObject, false)
oField.dispose()
Exit Do
End If
Loop
End Sub
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!
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