I have a dbs which will allow the user to export a query into an Excel file. After others have made changes to this file, the dbs will import it, and update the appropriate tables.
I have included vba code in the exported Excel file. (The MicroSoft Excel 9.0 Object Library must be referenced for this to work). When the excel file opens, it advises the user on which fields can be altered. The vba code includes a parameter which will allow the user to delete the code from the Excel file (so they don't have to look at the message everytime they open the file). Here's what I have so far.
Private Sub Cmd_Pull_Update_DEx_Click()
Form_Frm_Data_Export.Visible = False
If IsNull(COMPANY_ID) Then X = Format(Now, "yyyymmdd"
Else X = COMPANY_ID
DoCmd.OutputTo acOutputQuery, "Qry_Data_Export", acFormatXLS, "C:\My Documents\" & X & "_DBS_Data.xls", False
qualifier = X & "_DBS_Data.xls"
Call Export_Excel_VBA
Y = "Successfully exported data as ..." & Chr(10) & "C:\My Documents\" & X & "_DBS_Data.xls"
MsgBox Y, , "DBS dbs - Data Export"
Form_Frm_Data_Export.Visible = True
End Sub
Sub Export_Excel_VBA()
Dim ExApp As Excel.Application
Set ExApp = New Excel.Application
ChDir "C:\My Documents\"
X = "Public X As String" & Chr(10) & _
"Sub auto_open()" & Chr(10) & _
"on error resume next" & Chr(10) & _
"Call msg" & Chr(10) & _
"If X = ""Y"" Then" & Chr(10) & _
" For i = 3 To 21" & Chr(10) & _
" Application.VBE.SelectedVBComponent.CodeModule.DeleteLines 3" & Chr(10) & _
" Next i" & Chr(10) & _
"End If" & Chr(10) & _
"End Sub" & Chr(10) & _
"Sub msg()" & Chr(10) & _
"If ActiveSheet.Name = ""Qry_Data_Export"" Then" & Chr(10) & _
" a = ""Welcome to the DBS Export File."" & Chr(10) & Chr(10) & _" & Chr(10) & _
" ""Please feel free to make any updates you wish;"" & Chr(10) & _" & Chr(10) & _
" ""however, please do not..."" & Chr(10) & _" & Chr(10) & _
" "" 1. Delete Row 1"" & Chr(10) & _" & Chr(10) & _
" "" 2. Alter the Rec_# field (Column A)"" & Chr(10) & Chr(10) & _" & Chr(10) & _
" ""If you wish to mark a record for deletion,"" & Chr(10) & _" & Chr(10) & _
" ""please place a 'XX' in the Rec_Type field (Column C)."" & Chr(10) & Chr(10) & _" & Chr(10) & _
" ""Do you wish to remove this message from this file?""" & Chr(10) & _
" If MsgBox(a, vbYesNo, ""DBS - Import / Export File""
= 6 Then X = ""Y""" & Chr(10) & _
"End If" & Chr(10) & _
"End Sub"
With ExApp
.Workbooks.Open FileName:=qualifier
.VBE.ActiveVBProject.VBComponents.Add(vbext_ct_StdModule)
.VBE.CodePanes(1).CodeModule.AddFromString X
.ActiveWorkbook.Save
.Quit
End With
End Sub
My problem is that when 'Yes' is clicked on the msgbox, I receive an error. Once VBE is opened to debug, the code runs perfectly. I'm guessing that I need to include a line which opens VBE, but I haven't found the right line yet.
I would appreciate any input. Also, if there's a better way to export this vba code to Excel, please let me know.
Thanks in advance. -illini
I have included vba code in the exported Excel file. (The MicroSoft Excel 9.0 Object Library must be referenced for this to work). When the excel file opens, it advises the user on which fields can be altered. The vba code includes a parameter which will allow the user to delete the code from the Excel file (so they don't have to look at the message everytime they open the file). Here's what I have so far.
Private Sub Cmd_Pull_Update_DEx_Click()
Form_Frm_Data_Export.Visible = False
If IsNull(COMPANY_ID) Then X = Format(Now, "yyyymmdd"
DoCmd.OutputTo acOutputQuery, "Qry_Data_Export", acFormatXLS, "C:\My Documents\" & X & "_DBS_Data.xls", False
qualifier = X & "_DBS_Data.xls"
Call Export_Excel_VBA
Y = "Successfully exported data as ..." & Chr(10) & "C:\My Documents\" & X & "_DBS_Data.xls"
MsgBox Y, , "DBS dbs - Data Export"
Form_Frm_Data_Export.Visible = True
End Sub
Sub Export_Excel_VBA()
Dim ExApp As Excel.Application
Set ExApp = New Excel.Application
ChDir "C:\My Documents\"
X = "Public X As String" & Chr(10) & _
"Sub auto_open()" & Chr(10) & _
"on error resume next" & Chr(10) & _
"Call msg" & Chr(10) & _
"If X = ""Y"" Then" & Chr(10) & _
" For i = 3 To 21" & Chr(10) & _
" Application.VBE.SelectedVBComponent.CodeModule.DeleteLines 3" & Chr(10) & _
" Next i" & Chr(10) & _
"End If" & Chr(10) & _
"End Sub" & Chr(10) & _
"Sub msg()" & Chr(10) & _
"If ActiveSheet.Name = ""Qry_Data_Export"" Then" & Chr(10) & _
" a = ""Welcome to the DBS Export File."" & Chr(10) & Chr(10) & _" & Chr(10) & _
" ""Please feel free to make any updates you wish;"" & Chr(10) & _" & Chr(10) & _
" ""however, please do not..."" & Chr(10) & _" & Chr(10) & _
" "" 1. Delete Row 1"" & Chr(10) & _" & Chr(10) & _
" "" 2. Alter the Rec_# field (Column A)"" & Chr(10) & Chr(10) & _" & Chr(10) & _
" ""If you wish to mark a record for deletion,"" & Chr(10) & _" & Chr(10) & _
" ""please place a 'XX' in the Rec_Type field (Column C)."" & Chr(10) & Chr(10) & _" & Chr(10) & _
" ""Do you wish to remove this message from this file?""" & Chr(10) & _
" If MsgBox(a, vbYesNo, ""DBS - Import / Export File""
"End If" & Chr(10) & _
"End Sub"
With ExApp
.Workbooks.Open FileName:=qualifier
.VBE.ActiveVBProject.VBComponents.Add(vbext_ct_StdModule)
.VBE.CodePanes(1).CodeModule.AddFromString X
.ActiveWorkbook.Save
.Quit
End With
End Sub
My problem is that when 'Yes' is clicked on the msgbox, I receive an error. Once VBE is opened to debug, the code runs perfectly. I'm guessing that I need to include a line which opens VBE, but I haven't found the right line yet.
I would appreciate any input. Also, if there's a better way to export this vba code to Excel, please let me know.
Thanks in advance. -illini