|
Question : Compile error. variable not defined
|
|
In this form, there are three buttons: browse, export, cancel, and one text box.
When I click on the export button it did not export to an excel file. people is the name of an access table that needs to be exported to an excel spreadsheet
When I click on the browse button, an error message appear Compile error. variable not defined.
The computer place a yellow highlight at Private Sub BrowseFile(sDir As String)
The computer place a blue highlight at CommonDialog1.DialogTitle = "Save As Excel File"
////////////////////////////////
Option Compare Database Option Explicit Dim sFile As String
Private Sub cmdBrowse_Click() Dim sDir As String
'sDir = GetDBDir(Me.txtFile) sDir = GetDBDir("E:\APARTMENT MANAGEMENT SOFTWARE\Access Examples\Export_File.mdb") Debug.Print "sDir = "; sDir BrowseFile sDir End Sub
Function GetDBDir(Optional dbName As Variant) As String Dim i As Integer
If IsMissing(dbName) Or IsNull(dbName) Then Debug.Print dbName dbName = CurrentDb.Name End If
For i = Len(dbName) To 1 Step -1 If Mid(dbName, i, 1) = "\" Then Debug.Print "DBDir is " & Left(dbName, i) Exit For End If Next i
GetDBDir = Left(dbName, i) End Function
YELLOW HIGHLIGHT Private Sub BrowseFile(sDir As String) 'open dialog BLUE HIGHLIGHT CommonDialog1.DialogTitle = "Save As Excel File" CommonDialog1.Filter = "Excel File (*.xls)|*.xls" CommonDialog1.FileName = "" CommonDialog1.InitDir = sDir CommonDialog1.ShowOpen txtfile = CommonDialog1.FileName End Sub
Private Sub cmdCancel_Click() DoCmd.Close End Sub
Private Sub cmdExport_Click() Dim txtfile As String
txtfile = "Export.txt" ExportData txtfile DoCmd.Close acForm, "Export" End Sub
Private Sub Form_Open(Cancel As Integer) InsideHeight = 1440 InsideWidth = 7200 End Sub
Private Sub ExportData(sFile As String)
'people is the name of an access table 'that needs to be exported to an excel spreadsheet
DoCmd.SetWarnings False DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, "People", sFile, True MsgBox "Export Complete!", vbExclamation + vbOKOnly, Me.Caption DoCmd.SetWarnings True End Sub
|
|
Answer : Compile error. variable not defined
|
|
>>> CommonDialog1.DialogTitle = "Save As Excel File"
This means you are referencing an object that has not been declared. You are obviously using a common dialog control...what did you name the control on the form? If you copy/pasted this code from another project, you'll have to change the names (for the protection of the innocent! LOL!) to reflect the names you are using in your project.
|
|
|
|