Dim strSheetNames as string
Dim sht as Excel.Worksheet
For each sht in wbk.Worksheets
strSheetNames = strSheetNames & ";" & sht.name
Next
strSheetNames = mid(strSheetnames, 2)
'Set the RowSourceType to Value List
me.cbo_Worksheets.RowSource = strSheetNames
Private Sub Form_Open(Cancel As Integer)
On Error GoTo Error_Handler
Call LoadXlsSheetsCBO("C:\Test\Documents\Test.xlsx", Me.CBOCtrlName)
Error_Handler_Exit:
On Error Resume Next
Exit Sub
Error_Handler:
MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: Form_Open" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occured!"
Resume Error_Handler_Exit
End Sub
Private Sub LoadXlsSheetsCBO(ByVal sFile As String, CboCtrl As Access.Control)
Dim xlApp As Object
Dim xlWrkBk As Object
Dim xlWrkSht As Object
Dim ssheets As String
Dim i As Integer
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application") 'Bind to existing instance of Excel
If Err.Number <> 0 Then 'Could not get instance of Excel, so create a new one
Err.Clear
On Error GoTo Error_Handler
Set xlApp = CreateObject("excel.application")
Else
On Error GoTo Error_Handler
End If
xlApp.Visible = False 'make excel visible or not to the user
Set xlWrkBk = xlApp.Workbooks.Open(sFile)
For i = 1 To xlWrkBk.Sheets.Count
ssheets = ssheets & xlWrkBk.Sheets(i).Name & ";"
Next i
CboCtrl.RowSource = ssheets
Error_Handler_Exit:
On Error Resume Next
If Not xlWrkSht Is Nothing Then Set xlWrkSht = Nothing
If Not xlWrkBk Is Nothing Then
xlWrkBk.Close False
Set xlWrkBk = Nothing
End If
If Not xlApp Is Nothing Then
xlApp.Close
Set xlApp = Nothing
End If
Exit Sub
Error_Handler:
MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: LoadXlsSheetsCBO" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occured!"
Resume Error_Handler_Exit
End Sub
Option Compare Database
Private Sub ButBrowseSheet_Click()
On Error GoTo Error_Handler
Call LoadXlsSheetsCBO(Me.txtFileName, Me.Combo17)
Error_Handler_Exit:
On Error Resume Next
Exit Sub
Error_Handler:
MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: Form_Open" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occured!"
Resume Error_Handler_Exit
End Sub
Private Sub LoadXlsSheetsCBO(ByVal sFile As String, CboCtrl As Access.Control)
Dim xlApp As Object
Dim xlWrkBk As Object
Dim xlWrkSht As Object
Dim ssheets As String
Dim i As Integer
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application") 'Bind to existing instance of Excel
If Err.Number <> 0 Then 'Could not get instance of Excel, so create a new one
Err.Clear
On Error GoTo Error_Handler
Set xlApp = CreateObject("excel.application")
Else
On Error GoTo Error_Handler
End If
xlApp.Visible = False 'make excel visible or not to the user
Set xlWrkBk = xlApp.Workbooks.Open(sFile)
For i = 1 To xlWrkBk.Sheets.Count
ssheets = ssheets & xlWrkBk.Sheets(i).Name & ";"
Next i
CboCtrl.RowSource = ssheets
Error_Handler_Exit:
On Error Resume Next
If Not xlWrkSht Is Nothing Then Set xlWrkSht = Nothing
If Not xlWrkBk Is Nothing Then
xlWrkBk.Close False
Set xlWrkBk = Nothing
End If
If Not xlApp Is Nothing Then
xlApp.Close
Set xlApp = Nothing
End If
Exit Sub
Error_Handler:
MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: LoadXlsSheetsCBO" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occured!"
Resume Error_Handler_Exit
End Sub
Private Sub butImport_Click()
Dim FSO As New FileSystemObject
If Nz(Me.txtFileName, "") = "" Then
MsgBox "Please select a file"
Exit Sub
End If
If FSO.FileExists(Nz(Me.txtFileName, "")) Then
ExcelImport.ImportExcelSpreadSheet Me.txtFileName, "ImportedFile"
Else
MsgBox "File Not Found"
Exit Sub
End If
MsgBox "Import Of data was Successful", vbOKOnly, "AIMS Import"
End Sub
Private Sub buttBrowse_Click()
Dim diag As Office.FileDialog
Dim item As Variant
DoCmd.Hourglass True
Set diag = Application.FileDialog(msoFileDialogFilePicker)
diag.AllowMultiSelect = False
diag.Title = "Please Select Excel Spread Sheet"
diag.Filters.Clear
diag.Filters.Add "Excel Spreadsheets", "*.xls, *.xlsx"
If diag.Show Then
For Each item In diag.SelectedItems
Me.txtFileName = item
Next
DoCmd.Hourglass False
End If
End Sub
Private Sub Form_Open(Cancel As Integer)
Me.Caption = ApplicationCaption
End Sub