Solved

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

Posted on 2016-08-12
1
24 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
1 Comment
 
LVL 31

Accepted Solution

by:
Helen_Feddema earned 500 total points
Comment Utility
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server views 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 Microsoft Access…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now