I am trying to get the following code to use the sheet name that I choose on a list box in a DoCMD. TransferSpreadsheet path. The code works if I place the actual name in the path. But when I try to get it to read the cboText choice I get an error that it cannot find the object.Can someone please help me??? I am not very good with VBA.
Here is my code.
Here is my code.
Code:
Option Compare Database
Sub ImportExcel()
Dim Filepath As String
Dim cboText As String
cboText = [Forms]![frmUpdateCMIData]![cboParentLocation]
Filepath = "C:\Users\p418549\Desktop\CA CMI Data 2014-12-30 by facility tabs.xlsx"
If FileExist(Filepath) Then
DoCmd.TransferSpreadsheet acImport, , "ImportFromExcel", Filepath, False, "cboText!E9:P22"
Else
MsgBox "File not found. Please check filename or file location."
End If
MsgBox "File Transfer Successful"
Dim StrSql As String
Dim tblname As String
tblname = "ImportFromExcel"
StrSql = "ALTER TABLE [" & tblname & "] ADD COLUMN ID Counter"
DoCmd.RunSQL StrSql
End Sub
Function FileExist(sTestFile As String) As Boolean
'This function does not use DIR since it is possible that you might have
'been in the middle of running DIR against another directory in
'an attempt to match one directory against another.
'It does not handle wildcard characters
Dim lSize As Long
On Error Resume Next
'Preset length to -1 because files can be zero bytes in length
lSize = -1
'Get the length of the file
lSize = FileLen(sTestFile)
If lSize > -1 Then
FileExist = True
Else
FileExist = False
End If
End Function