I am tring to import an Excel file directly to a table in Access 2003. The code runs but it locks up the app and I need to do a ctrl/alt/del to get out. Here is the code:
Private Declare Function GetOpenFileName Lib "comdlg32.d ll" Alias _
"GetOpenFileNam eA" (pOpenfilename As OPENFILENAME) As Long
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilt er As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Private Sub ImportFile_Clic k()
On Error GoTo Err_ImportFile_ Click
Dim OpenFile As OPENFILENAME
Dim lReturn As Long
Dim sFilter As String
Dim WrksheetName As String
Dim i As Integer
Dim oApp As Object
OpenFile.lStruc tSize = Len(OpenFile)
OpenFile.hwndOw ner = Form.Hwnd
'OpenFile.hInst ance = App.hInstance
sFilter = "acSpreadsheetT ypeExcel9 (*.xls)" & Chr(0) & "*.xls" & Chr(0)
OpenFile.lpstrF ilter = sFilter
OpenFile.nFilte rIndex = 1
OpenFile.lpstrF ile = String(257, 0)
OpenFile.nMaxFi le = Len(OpenFile.lp strFile) - 1
OpenFile.lpstrF ileTitle = OpenFile.lpstrF ile
OpenFile.nMaxFi leTitle = OpenFile.nMaxFi le
OpenFile.lpstrI nitialDir = "C:\"
OpenFile.lpstrT itle = "Locate and Select the File for Import"
OpenFile.flags = 0
lReturn = GetOpenFileName (OpenFile)
Set oApp = CreateObject("E xcel.Applicatio n")
oApp.Visible = True
oApp.Workbooks. Open OpenFile.lpstrF ile
With oApp
.Visible = True
With .Workbooks(.Wor kbooks.Count)
For i = 1 To .Worksheets.Cou nt
WrksheetName = .Worksheets(i). Name
DoCmd.TransferS preadsheet acImport, cSpreadsheetTyp eExcel9, _
"AIS Release and Transport Status", OpenFile.lpstrF ile, True
Next i
End With
End With
Set oApp = Nothing
Screen.Previous Control.SetFocu s
DoCmd.FindNext
Exit_ImportFile _Click:
Exit Sub
Err_ImportFile_ Click:
MsgBox Err.Description
Resume Exit_ImportFile _Click
End Sub
I'am stuck as to what to try next. If anyone has an idea I'd sure like to here it. Thanks in advance.
Private Declare Function GetOpenFileName Lib "comdlg32.d ll" Alias _
"GetOpenFileNam eA" (pOpenfilename As OPENFILENAME) As Long
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilt er As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Private Sub ImportFile_Clic k()
On Error GoTo Err_ImportFile_ Click
Dim OpenFile As OPENFILENAME
Dim lReturn As Long
Dim sFilter As String
Dim WrksheetName As String
Dim i As Integer
Dim oApp As Object
OpenFile.lStruc tSize = Len(OpenFile)
OpenFile.hwndOw ner = Form.Hwnd
'OpenFile.hInst ance = App.hInstance
sFilter = "acSpreadsheetT ypeExcel9 (*.xls)" & Chr(0) & "*.xls" & Chr(0)
OpenFile.lpstrF ilter = sFilter
OpenFile.nFilte rIndex = 1
OpenFile.lpstrF ile = String(257, 0)
OpenFile.nMaxFi le = Len(OpenFile.lp strFile) - 1
OpenFile.lpstrF ileTitle = OpenFile.lpstrF ile
OpenFile.nMaxFi leTitle = OpenFile.nMaxFi le
OpenFile.lpstrI nitialDir = "C:\"
OpenFile.lpstrT itle = "Locate and Select the File for Import"
OpenFile.flags = 0
lReturn = GetOpenFileName (OpenFile)
Set oApp = CreateObject("E xcel.Applicatio n")
oApp.Visible = True
oApp.Workbooks. Open OpenFile.lpstrF ile
With oApp
.Visible = True
With .Workbooks(.Wor kbooks.Count)
For i = 1 To .Worksheets.Cou nt
WrksheetName = .Worksheets(i). Name
DoCmd.TransferS preadsheet acImport, cSpreadsheetTyp eExcel9, _
"AIS Release and Transport Status", OpenFile.lpstrF ile, True
Next i
End With
End With
Set oApp = Nothing
Screen.Previous Control.SetFocu s
DoCmd.FindNext
Exit_ImportFile _Click:
Exit Sub
Err_ImportFile_ Click:
MsgBox Err.Description
Resume Exit_ImportFile _Click
End Sub
I'am stuck as to what to try next. If anyone has an idea I'd sure like to here it. Thanks in advance.
Comment