Results 1 to 5 of 5
  1. #1
    Phred is offline Competent Performer
    Windows XP Access 2010 (version 14.0)
    Join Date
    Dec 2009
    Posts
    246

    Convert SearchForRecord Macro to VBA

    Access 2007, Not compiled, not split, Windows 7, Moderate VBA and Access skills.



    I have a main Customer form with a combo box. The combo box was created by the wizard. You select the Customer Name from the combo box and the corresponding record is populated on the main form. The wizard created the macro for the combobox. The macro is as follows:
    SearchForRecord, , First, ="[CustomerID] = " & Str(Nz(Screen.ActiveControl,0))

    RecordSource = SELECT tbl_customers.customerID, tbl_customers.customername FROM tbl_customers ORDER BY tbl_customers.customername;

    The combobox is unbound.

    I would like to convert this SearchForRecord macro over to VBA so I have better control over what happens. None of my four attempts work. I can't figure what I am doing wrong.
    1. DoCmd.SearchForRecord , , acFirst, "[CustomerID] = '" & Me.Combo80.Column(0) & "'"
    2. DoCmd.SearchForRecord , , acFirst, "[CustomerID] = '" & ActiveControl.Combo80.Column(0) & "'"
    3. DoCmd.SearchForRecord , , acFirst, "[CustomerID] = " & Str(Nz(Screen.ActiveControl, 0)) & "'"
    4. DoCmd.SearchForRecord acActiveDataObject, , acFirst, "[CustomerID] = '" & Me.Combo80.Column(0) & "'"
    Can anyone help?

    Thanks Phred

  2. #2
    ketbdnetbp is offline Competent Performer
    Windows 7 32bit Access 2003
    Join Date
    Mar 2011
    Location
    Midwest
    Posts
    254
    Phred - Untested in A2007 (works in A2003) but, you might be able to use the following vba in an after update event of the combo:

    Dim rs As Object
    Set rs = Me.Recordset.Clone
    rs.FindFirst "[CustomerID] = " & Me![Combo80]
    If Not rs.EOF Then Me.Bookmark = rs.Bookmark

    All the best,

    Jim

  3. #3
    Phred is offline Competent Performer
    Windows XP Access 2010 (version 14.0)
    Join Date
    Dec 2009
    Posts
    246
    Jim:

    I need to pick up Column(0) which is the CustomerID. This doesn't look like it is picking it up. Am I missing something?

    Thanks Fred

  4. #4
    ketbdnetbp is offline Competent Performer
    Windows 7 32bit Access 2003
    Join Date
    Mar 2011
    Location
    Midwest
    Posts
    254
    Fred - If the bound column on the combo's properties sheet is 1 and that points to the CustomerID, then you shouldn't have to specify the column. Have you tried the code? If it doesn't work, you could also try:

    Dim rs As Object
    dim x as Long
    x = Me![Combo80].Column(0)
    Set rs = Me.Recordset.Clone
    rs.FindFirst "[CustomerID] = " & x
    If Not rs.EOF Then Me.Bookmark = rs.Bookmark

    Hope this helps,

    Jim

  5. #5
    Phred is offline Competent Performer
    Windows XP Access 2010 (version 14.0)
    Join Date
    Dec 2009
    Posts
    246
    The code seems to work but I can't figure out why. You are right the bound column on the property sheet is 1 (CustomerID). Column(2) is Customer name.

    The thing that throws me is that everywhere else I have dedicated the column(0), This is new to me.

    Thank you for your help Jim

    Fred

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Can you convert .mdb to .wdb??
    By DeeMax45 in forum Access
    Replies: 2
    Last Post: 01-22-2012, 06:11 PM
  2. Convert Row to Column
    By albert_leung in forum Access
    Replies: 4
    Last Post: 10-16-2011, 11:31 AM
  3. Replies: 0
    Last Post: 01-12-2011, 12:43 PM
  4. How to convert a db to txt
    By Joliet_Jake in forum Import/Export Data
    Replies: 5
    Last Post: 11-01-2010, 12:07 AM
  5. Convert Excel Macro to Access Query
    By crownedzero in forum Queries
    Replies: 1
    Last Post: 06-22-2009, 02:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums