Results 1 to 3 of 3
  1. #1
    Mister-B is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jan 2018
    Posts
    31

    add Item to comboBox

    Hi there,

    I'm new to Access but I have a lot of experience with Excel and also with VBA in Excel.

    I'm trying to use VBA to achieve the following on an Access form:

    I have four ComboBoxes, with names "cboBew1" - "cboBew4". I also have another ComboBox "cboTN1". I am trying to get my macro to run through the 4 Boxes and add the displayed contents of these boxes to the drop down menu of cboTN1. Here's my code:

    Function Makro1()
    Dim i As Long
    With Forms("Daten - Formular")


    For i = 1 To 4
    strItem = .Controls("cboBew" & i).Value
    .cboTN1.AddItem Item:=strItem, Index:=0
    Next
    End With


    End Function

    When I run the code, cboTN1 is empty although there should be 4 entries. What am I doing wrong? Also how would I deal with the possibiliby of one of the 4 ComboBoxes being empty.



    Kind regards
    Martin

  2. #2
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,142
    I assume cboTN1 is set to be have a value list as it's rowsource.

    The normal route would be to make a query that referenced your other combo's and use that as the recordsource for the final combo.
    That can also deal with the null values automatically.

    What is the overall purpose of this - a search form ?
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    Gicu's Avatar
    Gicu is offline VIP
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,231
    Is the rowsourcetype property of thecboTN1 set to Value List? Try this:
    Code:
    Function Makro1()
    Dim i As Long,strItem as string
    With Forms("Daten - Formular")
    
    
    For i = 1 To 4
       strItem =nz( .Controls("cboBew" & i),"")
       if stritem<>"" then .cboTN1.AddItem Item:=strItem, Index:=0
    Next
    End With
    
    
    End Function
    Cheers,
    Vlad

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

Similar Threads

  1. select combobox item
    By civl_eng in forum Access
    Replies: 3
    Last Post: 05-15-2018, 02:49 AM
  2. show combobox item instead value
    By Milad Roohi in forum Access
    Replies: 1
    Last Post: 05-26-2016, 03:10 PM
  3. Replies: 1
    Last Post: 02-01-2015, 12:16 AM
  4. Replies: 1
    Last Post: 04-21-2014, 11:29 AM
  5. Selecting a combobox item with VBA
    By tylerg11 in forum Forms
    Replies: 6
    Last Post: 04-19-2012, 12:30 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