Opening form from ADO recordset result

I needed to change  the findrecord macro into VBA as i needed another VBA command in the after update event for a list box. I have been told that in order to achieve the results of the find record macro that, when an option of the listbox is selected, the whole record is opened in the form (The listbox is in the form in question) to use ADO, so far i have got:
Private Sub lstContacts_AfterUpdate()
Dim rst As ADODB.Recordset
Dim val As String
val = Me![lstContacts].Value
MsgBox (val)
Set rst = New ADODB.Recordset
rst.Open "SELECT * FROM Contacts", CurrentProject.Connection, adOpenKeyset, adLockOptimistic
rst.Find "[ContactID] ='" & val & "'"
If Not rst.EOF Then
'The record was found
     'Do Something Here
End If
rst.Close
End Sub
Firstly  i am unsure as to what is happening, secondly i a unsure as to how to actually open the form.

Thanks
Dan
Dan89Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
check out this:
Private Sub lstContacts_AfterUpdate()
Dim rst As ADODB.Recordset
Dim val As String
val = Me![lstContacts].Value
MsgBox (val)
Set rst = New ADODB.Recordset
rst.Open "SELECT * FROM Contacts WHERE [ContactID] ='" & val & "'", CurrentProject.Connection, adOpenKeyset, adLockOptimistic
If rst.EOF and rst.BOF Then
  ' record not found
Else
  'The record was found
  'Do Something Here
End If
rst.Close
End Sub

Open in new window

0
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Do you want to open a form based on the ContactID IF you find that record?

If so, then something like this:

Private Sub lstContacts_AfterUpdate()
Dim rst As ADODB.Recordset
Dim val As String
val = Me![lstContacts].Value
MsgBox (val)
Set rst = New ADODB.Recordset
rst.Open "SELECT * FROM Contacts", CurrentProject.Connection, adOpenKeyset, adLockOptimistic
rst.Find "[ContactID] ='" & val & "'"
If Not (rst.EOF And rst.BOF) Then
'The record was found
     DoCmd.OpenForm "NameOfYourForm", , , "ContactID='" & val & "'"
End If
rst.Close
End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dan89Author Commented:
Basically the listbox displays contact names within the contacts form, but its the contact ID that is remembered when a contact is selected from this list, that is the variable Val, when i select a contact from the list box i want the form to update to display all data for that contact exactly like a listbox does when you choose to "find a record based on your selection" in the list box wizard.

LSM your code gives me the error message 'Data type mismatch on the line:
DoCmd.OpenForm "NameOfYourForm", , , "ContactID='" & val & "'"

Thanks
Dan
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Dan89Author Commented:
angelll your code gives the same results, is that this way is specific to a string rather than an integer? ContactID is an autonumber, i have tried changing the data type of Var to an integer rather than a string but this has not worked either?

Thanks
Dan
0
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
If ContactID is a Numeric value, you don't enclose it in single quotes. Try this, with trimmed down code:

Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset

rst.Open "SELECT * FROM Contacts WHERE ContactID=" & Me.lstContacts.Column(0), CurrentProject.Connection

If Not (rst.EOF and rst.BOF) Then
  'The record was found
  DoCmd.OpenForm "NameOfYourForm", , , "ContactID=" & Me.lstContacts.Column(0)
End If

rst.Close

0
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Note that my suggestion assumes that the ContactID is in the FIRST column of lstContacts ... if not, you'd need to change that value.
0
Dan89Author Commented:
Ahh yes, that works perfectly, thanks a bunch!
If its not too much troube can you go through how that works as im new to this and would like to understand rather than just copy and paste.

Thanks again
Dan
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.