Passing a variable between forms

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Good Morning,
I am trying to pass information between 2 forms. Basically if one of
the fields equals a specific answer the another form will open and
autopopulate with values from certain fields. Here is what I have so far and
everything works until it is time to change the name of the most recently
opened form. Also you will see a nested if statement, I could not get an or
statement to work if you can help with that as well I would greatly
appreciate it.

Private Sub Command26_Click()

Dim v_name As String
Dim v_ticket As Long
If [Voice Order Type] = 1 Then
GoTo Run
Else
If [Voice Order Type] = 6 Then
GoTo Run
Else
GoTo Continue
End If
End If

Run:
v_name = DLookup("[Name]", "tbl_usernames", "[UserID] = UserNAme")
v_ticket = [Ticket #]
Text27 = v_name
DoCmd.OpenForm "MAC_LIST", , , , acFormAdd
Name = Forms("tbl_ticket_tracking").v_name 'This is where the
error is
[Ticket #] = v_ticket


Continue:

End Sub
 
Steve,

I'm trying to guess, but would be a lot easier if you specify:

* The name of the original form, on which Command26 is;
* The name of the other form ("MAC_LIST" ?);
* The name of the control(s) on the first form whose value(s) you want
to trasnfer along to the second form, and (corresponding) control names
on the second form;
* What is UserNAme? Where is it supposed to come from?

Nikos
 
Not quite sure exactly what you are assigning where but check my code below.
Are the fields you are assigning on the MAC_LIST form ?
If they are then you will need to put Forms![MAC_LIST]. in front

Private Sub Command26_Click()
Dim v_name As String
Dim v_ticket As Long
If [Voice Order Type] = 1 Or [Voice Order Type] = 6 Then
v_name = DLookup("[Name]", "tbl_usernames", "[UserID] = " & UserName)
v_ticket = [Ticket #]
Text27 = v_name
DoCmd.OpenForm "MAC_LIST", , , , acFormAdd
Name = Forms![tbl_ticket_tracking]).v_name 'This is where the
error is
[Ticket #] = v_ticket
end if
End Sub
 
Sorry about being unclear, Command26 is on a form called
"frm_ticket_tracking" I am assigning v_name the value of the field "UserNAme"
on that form and then, on the second form which is called "MAC_LIST" I am
trying to set the value of the field "Name" to be equal to v_name.
Private Sub Command26_Click()

Dim v_name As String
Dim v_ticket As Long
If [Voice Order Type] = 1 Then
GoTo Run
Else
If [Voice Order Type] = 6 Then
GoTo Run
Else
GoTo Continue
End If
End If

Run:
v_name = DLookup("[Name]", "tbl_usernames", "[UserID] = UserNAme")
v_ticket = [Ticket #]
Text27 = v_name
DoCmd.OpenForm "MAC_LIST", , , , acFormAdd
Name = Forms("tbl_ticket_tracking").v_name 'This is where the
error is
[Ticket #] = v_ticket


Continue:

End Sub
--
Thank You,
Steve


Nikos Yannacopoulos said:
Steve,

I'm trying to guess, but would be a lot easier if you specify:

* The name of the original form, on which Command26 is;
* The name of the other form ("MAC_LIST" ?);
* The name of the control(s) on the first form whose value(s) you want
to trasnfer along to the second form, and (corresponding) control names
on the second form;
* What is UserNAme? Where is it supposed to come from?

Nikos

Steve said:
Good Morning,
I am trying to pass information between 2 forms. Basically if one of
the fields equals a specific answer the another form will open and
autopopulate with values from certain fields. Here is what I have so far and
everything works until it is time to change the name of the most recently
opened form. Also you will see a nested if statement, I could not get an or
statement to work if you can help with that as well I would greatly
appreciate it.

Private Sub Command26_Click()

Dim v_name As String
Dim v_ticket As Long
If [Voice Order Type] = 1 Then
GoTo Run
Else
If [Voice Order Type] = 6 Then
GoTo Run
Else
GoTo Continue
End If
End If

Run:
v_name = DLookup("[Name]", "tbl_usernames", "[UserID] = UserNAme")
v_ticket = [Ticket #]
Text27 = v_name
DoCmd.OpenForm "MAC_LIST", , , , acFormAdd
Name = Forms("tbl_ticket_tracking").v_name 'This is where the
error is
[Ticket #] = v_ticket


Continue:

End Sub
 
Okay, I took Dennis's suggestion and it worked using the following code
except when it got to the name field I got an error about the field being
protected. The ticket number updated but the name didn't. I think it has
something to do with the fact that the Name field on the "MAC_LIST" form is a
combo box. Can I send a value to a combo box?

Private Sub Command26_Click()

Dim v_name As String
Dim v_ticket As Long
If [Voice Order Type] = 1 Then
GoTo Run
Else
If [Voice Order Type] = 6 Then
GoTo Run
Else
GoTo Continue
End If
End If

Run:
v_name = DLookup("[Name]", "tbl_usernames", "[UserID] = UserNAme")
v_ticket = [Ticket #]
Text27 = v_name
DoCmd.OpenForm "MAC_LIST", , , , acFormAdd
Forms![MAC_LIST].[Ticket #] = v_ticket
Forms![MAC_LIST].[Name] = v_name



Continue:

End Sub
 
Steve,

Try this:

Private Sub Command26_Click()
Dim v_name As String
Dim v_ticket As Long
If Me.[Voice Order Type] = 1 Or [Voice Order Type] = 6 Then
v_name = DLookup("[Name]", "tbl_usernames", "[UserID] = '" &
Me.UserName & "'")
v_ticket = Me.[Ticket #]
Me.Text27 = v_name
DoCmd.OpenForm "MAC_LIST", , , , acFormAdd
Forms("MAC_LIST").[Name] = v_name
Forms("MAC_LIST").[Ticket #] = v_ticket
End If
End Sub

As a general rule, you should not use Access reserved keywords like Name
for naming fields or controls, as this may easily produce problems. As
is, you have to always enclose Name in square brackets so Access/Jet/VBA
understands you are referring to an object, rather than a property
(which is what Name is in Access/Jet: every object has a Name property!).

HTH,
Nikos

Steve said:
Sorry about being unclear, Command26 is on a form called
"frm_ticket_tracking" I am assigning v_name the value of the field "UserNAme"
on that form and then, on the second form which is called "MAC_LIST" I am
trying to set the value of the field "Name" to be equal to v_name.

Private Sub Command26_Click()

Dim v_name As String
Dim v_ticket As Long
If [Voice Order Type] = 1 Then
GoTo Run
Else
If [Voice Order Type] = 6 Then
GoTo Run
Else
GoTo Continue
End If
End If

Run:
v_name = DLookup("[Name]", "tbl_usernames", "[UserID] = UserNAme")
v_ticket = [Ticket #]
Text27 = v_name
DoCmd.OpenForm "MAC_LIST", , , , acFormAdd
Name = Forms("tbl_ticket_tracking").v_name 'This is where the
error is
[Ticket #] = v_ticket


Continue:

End Sub
 
Back
Top