Open form and set value question

NWGal
2StarLounger
Posts: 198
Joined: 21 Aug 2011, 02:32

Open form and set value question

Post by NWGal »

I am using an unbound combo box to select a contact, then a button which opens a form for adding a record. The button has this code in the onclick event:

Private Sub Command438_Click()
If Len(Me.cbosrch & vbNullString) = 0 Then
Else
DoCmd.OpenForm "frmNewTeam", , , "[ConId] = " & Me!cbosrch.Column(0)
cbosrch = Null
End If
End Sub

Now I want to also have the contacts name set as the value of a textbox on the form. How do I append the code to do this?
Thanks

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Open form and set value question

Post by HansV »

Where do we get the name from? Is it the second column of cbosrch?
Best wishes,
Hans

NWGal
2StarLounger
Posts: 198
Joined: 21 Aug 2011, 02:32

Re: Open form and set value question

Post by NWGal »

Yes, ConId is column(0), so the next column should be the name. I get a little confused with the numbering of columns in combo boxes.

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Open form and set value question

Post by HansV »

You could use code like this:

Code: Select all

Private Sub Command438_Click()
    If Len(Me.cbosrch & vbNullString) > 0 Then
        DoCmd.OpenForm "frmNewTeam", , , "[ConId] = " & Me.cbosrch.Column(0)
        Forms!frmNewTeam!txtName = Me.cbosrch.Column(1)
        Me.cbosrch = Null
    End If
End Sub
where txtName is the name of the text box on frmNewTeam that you want to populate,
Best wishes,
Hans

NWGal
2StarLounger
Posts: 198
Joined: 21 Aug 2011, 02:32

Re: Open form and set value question

Post by NWGal »

Perfect, thank you much :)