Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MS VBA DoCMD Transferspreadsheet - Use form data to choose excel sheet - HELP 1

Status
Not open for further replies.

debq

Technical User
Aug 7, 2008
50
US
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.
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
 
try:

Code:
... False, cboText & "!E9:P22"

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
Thank You!!! That worked perfectly!!
 
I am glad, but do you know WHY this (yours)
[tt]False, "cboText!E9:p22"[/tt] did not work, but this (mine)
[tt]False, cboText & "!E9:p22"[/tt] worked OK?

I want you to understand it, so you can code it in the future.

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
I think your code works because cboText is a String data type that is being concatenated or added to the string "!E9:p22". In my code I had the two pieces together as one continuous string and the system could not find that full string as a sheet name and Range in the excel spreadsheet. Am I on the right path???
 
BINGO ! :)

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top