FileName Error

Tophan

Registered User.
Local time
Today, 18:57
Joined
Mar 27, 2011
Messages
374
I am trying to run a code that will print a report to pdf and save in the specified folder, however I keep getting an error with the file name. Where am I going wrong?

Also, is there a way of adding a hyperlink to the folder in the message box?


Code:
Private Sub CmdPrintJob_Click()
Dim strWhere As String

    If Me.Dirty Then    'Save any edits.
        Me.Dirty = False
    End If

    If Me.NewRecord Then 'Check there is a record to print
        MsgBox "Select a record to print"
    Else
        strWhere = "[ContractName] = """ & Me.[ContractName] & """"
        DoCmd.OpenReport "RPTJobSetUp", acViewPreview, , strWhere
    End If

Dim FileName As String
Dim FilePath As String

[COLOR="Red"]FileName = Me.ContractName
[/COLOR]FilePath = "J:\Job Set-Up Sheets\" & FileName & ".pdf"
DoCmd.OutputTo acOutputReport, "RPTJobSetUp", acFormatPDF, FilePath
Debug.Print FilePath
MsgBox "Your new job set-up has been successfully created and saved to J:\Job Set-Up Sheets ", vbInformation, "New Job Set-Up"

End Sub
 
What's in the textbox named ContractName? Offhand, a Null value will cause an error as a String variable can't hold Null. That control obviously has to exist as well.
 
Oh, and the error description is always handy. ;)
 
Hi.

The error description is Run-Time Error 2467: The expression you entered refers to an object that is closed or doesn't exist

The ContractName text box is a required field and primary key so there is always info in that field
 
Are you sure that's the name of the textbox, not the caption and not the field in the control source? The error implies that it doesn't exist on the form the code is in.
 
Yes...definitely the textbox name and not a caption. I thought I was misspelling it and even copied it from the properties box and pasted into the code and still got an error
 
Can you attach the db here?
 
The db won't upload...it is exceeding the max. file size and I am getting the below message

Your submission could not be processed because a security token was missing.

If this occurred unexpectedly, please inform the administrator and describe the action you performed before you received this error.

Would you accept a Dropbox link?
 
The database is 8MB even after the compress and repair. I will email it to you shortly.

Thanks.
 
Curious. I expected to find it had a different name, but you are correct. It seems to lose its identity between the line setting strWhere and that one. I put that line first and it seems to work fine:

Code:
Private Sub CmdPrintJob_Click()
Dim strWhere As String
Dim FileName As String
Dim FilePath As String

FileName = Me.ContractName
...

I'll have to ponder why this happens. By the way, the file would have been small enough to attach here if you zipped it.
 
Ah, I found the culprit. In the report that opens, you close this form in the open event and then open it again in the close event. Since that line was after the line opening the report, the form was "closed" even though its code was still running.
 
Oh...I think I understand. So I should keep the form open but maybe minimize it, let the report run and save to the folder and on closing the report, maximize/restore the form?
 
You could toggle its visible property. That would also leave it on the record it was on before. As is, it comes back to a new record (though perhaps that's what you want). It's okay to close/open it, you just need to get any values needed off of it before you close it. The variable held its value, but trying to refer to the form control results in an error.
 
I don't know how to toggle the visibility...sorry. I tried the minimize window and that seems to be working (sort of). No error messages and the file saved in the specified folder.

Is there a way to add a hyperlink to the folder in the message box?
 
I just mean you would set its Visible property to false and then back to true. I've never tried to put a hyperlink in the text, but offhand I doubt it would work. You could use a Yes/No message box that asks if the user wants to open the file, and open it if they answer yes.
 
Hi...Just giving you an update. So I made the changes to the code and added a couple lines to minimize the Job Set-Up form (see below in red).

The report opens and saves to the specified folder. On closing the report I created a simple dialog box that asks the user if they would like to view the file in the folder or if to return to the form.

Code:
Private Sub CmdPrintJob_Click()
Dim strWhere As String
Dim FileName As String
Dim FilePath As String

    If Me.Dirty Then    'Save any edits.
        Me.Dirty = False
    End If

    If Me.NewRecord Then 'Check there is a record to print
        MsgBox "Select a record to print"
    Else
        strWhere = "[ContractName] = """ & Me.[ContractName] & """"
        DoCmd.OpenReport "RPTJobSetUp", acViewPreview, , strWhere
        [COLOR="Red"]DoCmd.SelectObject acForm, Me.Name, False
        DoCmd.Minimize
[/COLOR]    End If

FileName = Me.ContractName
FilePath = "J:\Job Set-Up Sheets\" & FileName & ".pdf"
DoCmd.OutputTo acOutputReport, "RPTJobSetUp", acFormatPDF, FilePath
Debug.Print FilePath
MsgBox "Your new job set-up has been successfully created and saved to J:\Job Set-Up Sheets ", vbInformation, "New Job Set-Up"

End Sub


:D:D:D
 
Glad you got it working.
 

Users who are viewing this thread

Back
Top Bottom