Results 1 to 7 of 7
  1. #1
    Sys is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Apr 2015
    Posts
    4

    Import Excel Using VBA

    Can anyone point me in the right direction on how to import an Excel spreadsheet into a form listbox using VBA?
    Thanks.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    You don't need vba, but the list box is filled from a query/table, not an import.

    in a macro
    transfersreadsheet

    or vba: docmd.transferspreadsheet

  3. #3
    Sys is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Apr 2015
    Posts
    4
    Thanks for the reply.
    Yeah I was thinking about it the wrong way. I was able to import the spreadsheet to a table using the docmd.transferspreadsheet upon button click within a form. Now I am trying to get another form's listbox to populate from the table using Me.Listbox1.RowSource = "SELECT * FROM [Inventory]". Still think I am missing something though.

  4. #4
    Sys is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Apr 2015
    Posts
    4
    This is what I am experimenting with as of now:

    Private Sub Form_Initialize()


    Dim strTable As String
    strTable = Me.Listbox1
    Me.Listbox1.RowSourceType = "Table/Query"
    Me.Listbox1.RowSource = "SELECT * FROM Inventory"
    Me.Listbox1.Requery

    End Sub

  5. #5
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    No sys, you dont need any vb code. (remove your form_init code)
    The controls work from the form with zero code.
    If you import from the Excel file, then open the form, the control will populate itself, as long as you have the sql in the control property like you mentioned.

    If you import data while the form is open, then it list/combo must be refreshed using Me.Listbox1.Requery,
    or close the form, and reopen.

  6. #6
    Sys is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Apr 2015
    Posts
    4
    Unfortunately I have to use VBA because it is a school project that requires it. We had to create a form with a button that will delete records in a table and repopulate it with a spreadsheet (Which I have accomplished). And another form that populates a listbox with the data from that same table which I am trying to figure out. I'm not getting any errors but my listbox is blank.

  7. #7
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by Sys View Post
    ... I'm not getting any errors but my listbox is blank.
    The ListBox gets its data from the table. After the import process (next line of code) .Requery the Listbox. See if that works.

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

Similar Threads

  1. Replies: 10
    Last Post: 06-16-2014, 08:37 AM
  2. Import access query data into excel file, using excel vba
    By exceljockey in forum Import/Export Data
    Replies: 3
    Last Post: 11-15-2013, 02:36 PM
  3. Replies: 4
    Last Post: 12-17-2012, 01:21 PM
  4. Replies: 2
    Last Post: 02-14-2012, 04:40 PM
  5. VBA to open excel, import on close of excel
    By bdaniel in forum Programming
    Replies: 0
    Last Post: 03-20-2010, 02:45 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