Solved

Access: How to set the selected value of combo box when opening form from code

Posted on 2016-08-12
1
67 Views
Last Modified: 2016-08-12
I have a form with search results in a listbox, and I'd like to open the next form  with the chosen id prepopulated form the search. How do I do that?
Private Sub ContractNew_btn_Click()
    If (Me.PersonSearch_lst.Value > -1) Then
        Dim customerID As Integer
        customerID = Me.PersonSearch_lst.Value
        DoCmd.OpenForm "Order", acNormal
        DoCmd.GoToRecord acDataForm, "Order", acNewRec
        DoCmd.GoToControl "Customers_cbo"
        '"set Customers_cbo.Value = customerID"
    End If
    
End Sub

Open in new window


TIA
Dennis
0
Comment
Question by:Dennis_Gundersen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 31

Accepted Solution

by:
Helen_Feddema earned 500 total points
ID: 41754135
It might make more sense to run this code from the AfterUpdate event of the listbox but it should work from a button as well.  You need to set a Form variable to the newly opened form, and then set the value of the control on the form.  Try these changes (assuming that frm has been declared as Access.Form):

        DoCmd.OpenForm "Order", acNormal
        DoCmd.GoToRecord acDataForm, "Order", acNewRec
        Set frm = Forms("Order")
        frm![Customers_cbo].Value = customerID"

Open in new window


Also, if you set DataEntry to Yes for the Orders form, you don't need to go to the new record.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question