Option Explicit repairs - unknown variable?

fraser_lindsay

Access wannabe
Local time
Today, 11:23
Joined
Sep 7, 2005
Messages
218
Hi,

I'm after another short lesson again from somebody please. I'm trying to improve performance and as such have been 'retro-fitting' the phrase 'option explicit' to my code. I also enabled the option in tools.

Only one form has caused a problem and this is the offending code:

Code:
Private Sub SavePDF_Click()
'This code will open the named report and match the named fields below to open the report on that specific record

''Stop  ' This will halt the vba code.  You can step through each code using the F8 function key

Dim sReportName As String
    Dim sFilter As String

   
    sReportName = "rptSpecificJSA"
    sFilter = "JSARef = " & Me.cboJSARef
    
    sName = Me.cboJSARef.Column(1)
    Debug.Print sName  ' what is assigned to the variable will show in the immediate window

    sName2 = Me.cboJobGroup.Column(1)
    Debug.Print sName  ' what is assigned to the variable will show in the immediate window

    DoCmd.RunCommand acCmdSaveRecord
    DoCmd.OpenReport sReportName, acPreview, , sFilter
  
Dim stDefault As String  ' Default file location
Dim stFolder As String  '  Sub Folder of default location
Dim stExtend As String ' File extension
Dim stFilePath As String ' Build full path of all variables
Dim stDocName As String ' query to export

stDocName = "rptSpecificJSA"

stDefault = "U:\"
stFolder = "\GEHS Compliance\Group EHS Dept Folder\009  OCCUPATIONAL HEALTH\Industrial Hygiene\JSA Database\JSA PDF Archive\"


stPrefix = "GEHS JSA " & sName & " " & sName2 & " " & format$(Date, "ddmmyyyy") & " " & format$(Time, "hhmm")

stExtend = ".pdf"

stFilePath = stDefault & stFolder & stPrefix & stExtend
  
DoCmd.OutputTo acOutputReport, stDocName, acFormatPDF, stFilePath, False, acExportQualityPrint
''The True/False option here will load Adobe after it creates the PDF when 'true is selected'
    
DoCmd.Close acReport, "rptSpecificJSA"
   
MsgBox "The current JSA record has been exported to PDF"
    



End Sub

It trips up on 'sName'. Thta is used to pull two fields together to make up my PDF filename. Now, being an amateur I had no idea that wasn't a recognised variable.

How do I fix this to please "option explicit' and not screw up my PDF file export.

(This is Access 2007 by the way, which is why PDF works)

Thanks,

Fraser
 
If you click on the variable sName then right click and select Definition you will see where it was defined, if at all. If the later then simply declare it or some other variable name within the sub itself.

David
 
Dim sReportName As String
Dim sFilter As String


sReportName = "rptSpecificJSA"
sFilter = "JSARef = " & Me.cboJSARef

sName = Me.cboJSARef.Column(1)
Debug.Print sName ' what is assigned to the variable will show in the immediate window

sName2 = Me.cboJobGroup.Column(1)
Debug.Print sName ' what is assigned to the variable will show in the immediate window

option explicit tells the compiler that all variables MUST be declared before using them - this is good, as it prevents you making inadvertent errors with variables

in fact both sname and sname2 are used without being declared

so you need to add

dim sname as string
dim sname2 as string

with the other variables (blue at the top)
 

Users who are viewing this thread

Back
Top Bottom