gglgokop1704
Programmer
Dear All,
Please how can I add two extra fields to existing excell whorksheet and populate them with the workbook name and worksheet name respectively (programmatically).
I have several excel files that contain sevel worksheets. I want to transfer the worksheets from each work book to access and craete two columns which will be populated with the workbook names (Cost Centers) and worksheet names (General Ledgers). I have succeeded in trasfering the data and creating the two columns in access using TransferSpreadsheet method and ALTER table statement. The problem is that I could not populate the two columns after transfering to access using INSERT INTO or UPDATE statements. I am thinking if it is wiser to create the 2 columns in excel and populate them first before importing them to access.
Please any idea?
See my code below:
Private Sub Command7_Click()
Dim xlApp As Excel.Application
Dim xlWS As Excel.Worksheet
Dim xlWB As Excel.Workbook
Dim i As Integer
Dim strFileName As String
Dim wkShName As String
Dim strFolderPath As String
Dim strPath As String
Dim strPathBrowser As String
Dim bookName As String
Dim strFileNameValue As String
Dim strFullPath As String
Dim j As Integer
Set xlApp = New Excel.Application
On Error Resume Next
strPath = "C:\Documents and Settings\a99858\My Documents\"
strFileName = Dir(strPath & "*.xls")
strFullPath = strPath & strFileName
Do While Len(strFileName) > 0
strFullPath = strPath & strFileName
strFileNameValue = strFileName
xlApp.Workbooks.Open (strFullPath)
For j = 1 To xlApp.Worksheets.count
Set xlWS = xlApp.ActiveWorkbook.Worksheets(j)
wkShName = xlWS.Name
DoCmd.TransferSpreadsheet acImport, , "MultiSheet_Example", strFullPath, -1,
wkShName & "!A1:F8"
DoCmd.RunSQL "ALTER TABLE MultiSheet_Example ADD COLUMN CCCode CHAR, GCode
CHAR", -1
DoCmd.RunSQL "INSERT INTO MultiSheet_Example (CCCode ,GCode) VALUES (&
strFileNameValue, & wkShName)"
Next j
strFileName = Dir()
Loop
End Sub
Any help will be appreciated
Gokop
Please how can I add two extra fields to existing excell whorksheet and populate them with the workbook name and worksheet name respectively (programmatically).
I have several excel files that contain sevel worksheets. I want to transfer the worksheets from each work book to access and craete two columns which will be populated with the workbook names (Cost Centers) and worksheet names (General Ledgers). I have succeeded in trasfering the data and creating the two columns in access using TransferSpreadsheet method and ALTER table statement. The problem is that I could not populate the two columns after transfering to access using INSERT INTO or UPDATE statements. I am thinking if it is wiser to create the 2 columns in excel and populate them first before importing them to access.
Please any idea?
See my code below:
Private Sub Command7_Click()
Dim xlApp As Excel.Application
Dim xlWS As Excel.Worksheet
Dim xlWB As Excel.Workbook
Dim i As Integer
Dim strFileName As String
Dim wkShName As String
Dim strFolderPath As String
Dim strPath As String
Dim strPathBrowser As String
Dim bookName As String
Dim strFileNameValue As String
Dim strFullPath As String
Dim j As Integer
Set xlApp = New Excel.Application
On Error Resume Next
strPath = "C:\Documents and Settings\a99858\My Documents\"
strFileName = Dir(strPath & "*.xls")
strFullPath = strPath & strFileName
Do While Len(strFileName) > 0
strFullPath = strPath & strFileName
strFileNameValue = strFileName
xlApp.Workbooks.Open (strFullPath)
For j = 1 To xlApp.Worksheets.count
Set xlWS = xlApp.ActiveWorkbook.Worksheets(j)
wkShName = xlWS.Name
DoCmd.TransferSpreadsheet acImport, , "MultiSheet_Example", strFullPath, -1,
wkShName & "!A1:F8"
DoCmd.RunSQL "ALTER TABLE MultiSheet_Example ADD COLUMN CCCode CHAR, GCode
CHAR", -1
DoCmd.RunSQL "INSERT INTO MultiSheet_Example (CCCode ,GCode) VALUES (&
strFileNameValue, & wkShName)"
Next j
strFileName = Dir()
Loop
End Sub
Any help will be appreciated
Gokop