Results 1 to 10 of 10

Thread: How to load list box from Access Database

  1. #1
    New Member
    Join Date
    Jul 12
    Posts
    5

    Question How to load list box from Access Database

    Hi !!

    I want to load the data into List Box from Access Data base

    How can i set it dynamically, so that i can select the values in the list box

  2. #2
    Super Moderator Hack's Avatar
    Join Date
    Aug 01
    Location
    Searching for mendhak
    Posts
    58,283

    Re: How to load list box from Access Database

    Welcome to the forums.

    What programming language are you using? Access VBA?
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  3. #3
    New Member
    Join Date
    Jul 12
    Posts
    5

    Wink Re: How to load list box from Access Database

    Quote Originally Posted by Hack View Post
    Welcome to the forums.

    What programming language are you using? Access VBA?
    Hi Hack,

    Its in Excel User Forms List Box i want to load the data from Excel...

    Code:
    Dim db As Database, rs As Recordset, r As Long, dt As String
            Set db = OpenDatabase("C:\Invoice\Invoice.mdb")
          Set rs = db.OpenRecordset("InvoiceTbl", dbOpenTable)  
       With rs
        .AddNew ' create a new record
                dt = invmon.Value & "-" & invdt.Value & "-" & invyear.Value
                .Fields("InvoiceDate") = dt
              .Fields("CompanyID") = 1
                ' add more fields if necessary...
                .Update ' stores the new record
        End With    
        rs.Close
        Set rs = Nothing
        db.Close
        Set db = Nothing
        MsgBox "Data has been saved in access"
    I am using the above code to store the data to Access From Excel, Similarly I am looking for info on how to load the data from Access to Excel Forms List Box.

  4. #4
    New Member
    Join Date
    Jul 12
    Posts
    5

    Question Re: How to load list box from Access Database

    Hi !!

    I am using Excel Forms and want to load the data from Access to List Box and data can be selected from the List Box in forms.

    Code:
    Dim db As Database, rs As Recordset, r As Long, dt As String
        ' connect to the Access database
        ' Set cn = New ADODB.Connection
            Set db = OpenDatabase("C:\Invoice\Invoice.mdb")
        ' open the database
        Set rs = db.OpenRecordset("InvoiceTbl", dbOpenTable)
        
       With rs
        .AddNew ' create a new record
                ' add values to each field in the record
                dt = invmon.Value & "-" & invdt.Value & "-" & invyear.Value
                .Fields("InvoiceDate") = dt
              .Fields("CompanyID") = 1
                ' add more fields if necessary...
                .Update ' stores the new record
        End With
        
        rs.Close
        Set rs = Nothing
        db.Close 
        Set db = Nothing
        MsgBox "Data has been saved in access"
    I am using above code to store the data into Access from Excel...

    Now i want to show the data to List Box of User Forms by getting the data from Access DB.

  5. #5
    Super Moderator Hack's Avatar
    Join Date
    Aug 01
    Location
    Searching for mendhak
    Posts
    58,283

    Re: How to load list box from Access Database

    Excel VBA question moved to Office Development
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  6. #6
    New Member
    Join Date
    Jul 12
    Posts
    5

    Unhappy Re: How to load list box from Access Database

    Team,

    Any updates on this request... How can i show the Access data in the Excel List Box of User Forms... I had posted the code which i am using to store the data from Excel to List Box but how could i do the reverse of it...

  7. #7
    PowerPoster
    Join Date
    Dec 04
    Posts
    18,526

    Re: How to load list box from Access Database

    vb Code:
    1. Set db = OpenDatabase("C:\Invoice\Invoice.mdb")
    2.     ' open the database
    3.     Set rs = db.OpenRecordset("InvoiceTbl", dbOpenTable)
    4.       ' first part stays the same
    5.     do until rs.eof
    6.      listbox1.additem rs(f)   ' where f is the index of the field to fill the listbox
    7.      rs.movenext
    8.   loop
    alternatively you can use an sql query to populate the recordset, with only the desired results from the table
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  8. #8
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 07
    Location
    India
    Posts
    2,156

    Re: How to load list box from Access Database

    sql query something like this to open only where the field name 'FirstName' has 'seenu'
    Code:
    Set rs = db.OpenRecordset("select * from MyTable where FirstName='seenu'")
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  9. #9
    New Member
    Join Date
    Jul 12
    Posts
    5

    Re: How to load list box from Access Database

    Hi Seenu,

    I was trying to access using the SQL but was getting TypeMismatch issue....

    Code:
    Dim db As Database, rs As Recordset, r As Long
    Set db = OpenDatabase("C:\Invoice\Invoice.mdb")
        Set rs = db.OpenRecordset("select BName from BrokerTbl")
    Do Until rs.EOF
         Invoice_Preparation.brokername.AddItem rs
         rs.MoveNext
         Loop  
        rs.Close
        Set rs = Nothing
        db.Close
        Set db = Nothing

  10. #10
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 07
    Location
    India
    Posts
    2,156

    Re: How to load list box from Access Database

    kiran, try like this
    Code:
    Invoice_Preparation.brokername.AddItem rs!BName
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •