I have an Access 2010 Form that opens a query through OnLoad. While both forms and query are open, I want to populate a field from query into a txtfield on Form. How can i achieve this. a practical example of code through a click on command button would explain it to me better. Thanks - Ajay
If the query and form datasources are the same (eg same table or query) then set the control source of the txtfield on the form to the field from the query.
If not then you would need to add dlookup Function code (after the code that opens the query) to lookup the field you want to return the value from.
If the query and form datasources are the same (eg same table or query) then set the control source of the txtfield on the form to the field from the query.
If not then you would need to add dlookup Function code (after the code that opens the query) to lookup the field you want to return the value from.
Thanks Isskint
My form contains several free txt fields and with the help of button, i run various commands, on the other hand the query that opens with this form is from a different set of code but one of its field is input required in the form. i hope it helps, if not see the attached image of what i am trying to achieve.
Best Regards
Ajay
You should be able to write directly to the field but I would probably create a public property on the form you want to update.
In this example assume that the form is called "Form2Update" and the text field is "txtField2Update":
Code:
Public Property Let Field2Update(value As String)
txtField2Update.value = value
End Property
Then create a sub in the form that will update the form Form2Update form which can be called from whichever event you need to put it in:
Code:
Private Sub SynchForms()
On Error GoTo ErrorHandler
Dim f As Form_Form2Update
Set f = Forms("Form2Update")
f.Field2Update = "Whatever" ' or Me!QueryField etc.
ExitHandler:
Set f = Nothing
Exit Sub
ErrorHandler:
'Ignore the error if the form to synch is not loaded
If Err.Number <> 2450 Then
MsgBox Err.Description
End If
Resume ExitHandler
End Sub
This is not the shortest code you could use, it is just how I would do it.
You should be able to write directly to the field but I would probably create a public property on the form you want to update.
In this example assume that the form is called "Form2Update" and the text field is "txtField2Update":
Code:
Public Property Let Field2Update(value As String)
txtField2Update.value = value
End Property
Then create a sub in the form that will update the form Form2Update form which can be called from whichever event you need to put it in:
Code:
Private Sub SynchForms()
On Error GoTo ErrorHandler
Dim f As Form_Form2Update
Set f = Forms("Form2Update")
f.Field2Update = "Whatever" ' or Me!QueryField etc.
ExitHandler:
Set f = Nothing
Exit Sub
ErrorHandler:
'Ignore the error if the form to synch is not loaded
If Err.Number <> 2450 Then
MsgBox Err.Description
End If
Resume ExitHandler
End Sub
This is not the shortest code you could use, it is just how I would do it.
Thanks,
This is exactly what i have written in my form.
Public Property Let SLBatch(value As String)
txtslbatch.value = value
End Property
Private Sub SyncForms()
On Error GoTo ErrorHandler
Dim f As [Form_Date Selector]
Set f = Forms("[Date Selector]")
f.SLBatch = Me!wip_batches.[Last S/L BATCH]
Exithandler:
Set f = Nothing
Exit Sub
ErrorHandler:
'Ignore the error if the form to synch is not loaded
If Err.Number <> 2450 Then
Msg Box(Err.Description)
End If
Resume Exithandler
End Sub
Private Sub PopulatedBatches_Click()
SyncForms
End Sub
and when i try to Call it it by clicking PopulatedBatches Button with click then i am getting the error as per the attached image.
Did you put the property on the [Date Selector] and the rest of the code in the form that updates [Date Selector]? If so, try to single step through to see where it fails and let me know.