PDA

View Full Version : Solved: Code to carry data from one form to another not working



crowfan
09-20-2007, 06:41 AM
Hello,

I am having a problem with carrying data from one form to another.

Here is the setup: I have a Users table, a Positions table, and a UserPosition table, which is a join table, that I use to "assign" Positions to Users.

I also have a form called f_user_position. On that form is a drop-down list, which looks up values in the Users table and stores the selected value (UserID) for later. This drop-down is called Combo0. There is also a subform on the form that shows the associated positions. I have an AfterUpdate event on the drop-down which restricts the records shown in the subform based on the selection in the drop-down list.

I also have another, very simple form called UserPosition for creating new User-Position associations. You simply select the User (binding the UserID field) from one drop-down and the Position from another and the association is created. The User drop-down is coming from a Lookup Wizard field in the table itself.

I've added a button on the first form that opens the second form. It works fine, but I wanted to pre-fill the User drop-down on the second form with whatever User was selected on the first form. I have the following code in the OnClick event for the button:

Private Sub Command7_Click()
On Error GoTo Err_Command7_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "UserPosition"
stLinkCriteria = "[UserID]=" & CLng(Me.Combo0.Value)

DoCmd.OpenForm stDocName, , , stLinkCriteria
'MsgBox stLinkCriteria

Exit_Command7_Click:
Exit Sub

Err_Command7_Click:
MsgBox Err.Description
Resume Exit_Command7_Click

End Sub
The second form opens but the UserID field is not prefilled. I have used this code elsewhere and it works, however, in that case the value is a string. As you can see, I'm using the CLng function to make sure the value that's passed on is a Long. I've also tried Cint, I've tried adding single quotes, just pulling with value without CLng or CInt....nothing works.

The Msgbox line, which is commented out, is something I added in to see if the correct UserID value is being pulled. It is.

Any ideas are appreciated. Thanks!

crowfan
09-20-2007, 07:26 AM
I was wasting too much time on this problem, so I just redesigned the form. This problem is no longer an issue.