pass value from one control to another

D

Dave

I would like to pass the values in two text boxes from one form to the
text boxes of a second form. A button will open that second form and
insert the values in the particular boxes.

However, if the second form is opened on its own I want the same text
boxes to remain blank for user input. This is probably pretty easy but
I can't figure out the code or where to put it. any help would be
appreciated.

Dave
 
A

Arvin Meyer [MVP]

I would use the second form's Current event which will fire at each record
change. If you only want to change the value of new records, use the
DefaultValue property, if you want to change the textboxes regardless of
whether it's a new record, then use the second statement. I'm only going to
include the first textbox, the second one use the same code, but obviously
different names. This code will use the IsLoaded function found in the
Northwind sample database that comes with Office:

Function IsLoaded(ByVal strFormName As String) As Boolean
' Returns True if the specified form is open in Form view or Datasheet
view.
Dim oAccessObject As AccessObject

Set oAccessObject = CurrentProject.AllForms(strFormName)
If oAccessObject.IsLoaded Then
If oAccessObject.CurrentView <> acCurViewDesign Then
IsLoaded = True
End If
End If

End Function

'air code:
Sub Form_Current()
If IsLoaded("Form1") Then
' Me.TextBox1.DefaultValue = Forms!Form1!TextBox1
Me.TextBox1 = Forms!Form1!TextBox1
End If
End Sub

Comment/uncomment the appropriate line and add some error handling, to make
it more robust.
 
G

Guest

This is very simple if you don't overthink it.

Simply make the on_click event of the button open your second form and
assign the values from your first form's textboxes to your second's. You
need no code on your second form ( at least for this problem ) at all.

The code would be:

Private Sub Command3_Click()
Call DoCmd.OpenForm("Second Form")
Forms![SECOND FORM]![TEXTBOX1] = Me("TEXTBOX1")
Forms![SECOND FORM]![TEXTBOX2] = Me("TEXTBOX2")
End Sub
 
D

Dave

This is very simple if you don't overthink it.

Simply make the on_click event of the button open your second form and
assign the values from your first form's textboxes to your second's. You
need no code on your second form ( at least for this problem ) at all.

The code would be:

Private Sub Command3_Click()
Call DoCmd.OpenForm("Second Form")
Forms![SECOND FORM]![TEXTBOX1] = Me("TEXTBOX1")
Forms![SECOND FORM]![TEXTBOX2] = Me("TEXTBOX2")
End Sub


Thanks, but I get a "object doesn't support this property or method"
error

Dave
 
D

Dave

Nevermind. Just a ! in the wrong place. It works, and you're right
that was easy.

Thanks,
Dave
 
G

Guest

Did you modify the code to reflect the names of your forms and controls?

Dave said:
This is very simple if you don't overthink it.

Simply make the on_click event of the button open your second form and
assign the values from your first form's textboxes to your second's. You
need no code on your second form ( at least for this problem ) at all.

The code would be:

Private Sub Command3_Click()
Call DoCmd.OpenForm("Second Form")
Forms![SECOND FORM]![TEXTBOX1] = Me("TEXTBOX1")
Forms![SECOND FORM]![TEXTBOX2] = Me("TEXTBOX2")
End Sub


Thanks, but I get a "object doesn't support this property or method"
error

Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top