Connecting Tech Pros Worldwide Help | Site Map

Calling A Combobox Value in VBA Code

Newbie
 
Join Date: Jul 2007
Posts: 1
#1: Jul 31 '07
Hello,

I am using Access 2007, but need to be compatible with 2003. I am importing a sheet from Excel into Access (code below). I would like the sheet name to be determined from a drop down on a form instead of being hard coded. So the user would select the sheet name from the drop down, click Import, and the VBA would import the tab the user selected. Also, different tabs would need to be imported to different tables. Is there a way to make the table name dependent on the tab selected?

I'm attempting to use the same function instead of having it repeated a bunch of times with only two small variables changed.

Thanks!

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. '------------------------------------------------------------
  3. ' Import
  4. '
  5. '------------------------------------------------------------
  6. Function Import()
  7. Dim sFilename As Office.FileDialog
  8.  
  9. Set dlgOpen = Application.FileDialog(dialogtype:=msoFileDialogOpen)
  10.  
  11. With dlgOpen
  12.     .AllowMultiSelect = False
  13.     .Show
  14. End With
  15.  
  16. If dlgOpen.SelectedItems.Count = 0 Then Exit Function
  17. sPath = dlgOpen.SelectedItems.Item(1)
  18.  
  19. On Error GoTo Import_Err
  20.  
  21.     DoCmd.SetWarnings False
  22.     DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "tempTblWeeklyPipeline", sPath, True, "SAP OM Active$"
  23.     DoCmd.SetWarnings True
  24.     MsgBox "Import Complete"
  25.  
  26. Import_Exit:
  27.     Exit Function
  28.  
  29. Import_Err:
  30.     MsgBox Error$
  31.     Resume Import_Exit
  32.  
  33. End Function
msquared's Avatar
Administrator
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 10,865
#2: Aug 6 '07

re: Calling A Combobox Value in VBA Code


Set the tablename to a variable and use a case statement to set the value dependent on the tab.

If you need it to be compatible in 2003 then design it in 2003 not 2007. The backwards compatibility from 2007 to 2003 is not good.
Reply