Checking if file exists Type mismatch (1 Viewer)

Hamish 237

New member
Local time
Tomorrow, 04:45
Joined
Jun 19, 2019
Messages
16
Hi All im having issues with this bit of code can anyone assist with what i'm missing / doing wrong hopefully its something simple im just over looking or not familiar with I'm getting a Type Mismatch alert when i run the code. checking for a vbstring in the directory is where i think i'm getting it wrong

Code Tags Added by UG

Code:
Private Sub Command20_Click()
On Error GoTo Command20_Click_Err
Dim Filepath As String
Dim strname As String

'Look for windows directory file path to see if it exists
'If it doesn't exist creates the directory file folder then saves Pdf file in it
'If directory exists check if file already present if file already exists alert operator file will be overwritten
'

Filepath = Application.CurrentProject.Path & "\" & Me.VHID.Column(1) & "\" & "Fault" & "\" & Me.CLID
strname = Application.CurrentProject.Path & "\" & Me.VHID.Column(1) & "\" & "Fault" & "\" & Me.CLID & "\" & Me.CLID & ".pdf"
'question does directory exists as per filepath


    If Dir(Filepath, vbDirectory) = vbNullString Then
   
    MkDir Filepath
    DoCmd.OutputTo acOutputReport, "rClaimSUB", acFormatPDF, strname, False, , , acExportQualityPrint
    Application.FollowHyperlink Filepath

   
    Else
   
        If Dir(strname, vbNormal) = vbString Then
            If MsgBox("This file already exists do you want to replace file?", vbQuestion + vbYesNo, "Replace file") = vbYes Then
   
            DoCmd.OutputTo acOutputReport, "rClaimSUB", acFormatPDF, strname, False, , , acExportQualityPrint
            pplication.FollowHyperlink Filepath
   
            Else
            'Do Nothing
            End If
        Else
        DoCmd.OutputTo acOutputReport, "rClaimSUB", acFormatPDF, strname, False, , , acExportQualityPrint
        Application.FollowHyperlink Filepath
        End If

   
    End If

   

Command20_Click_Exit:
    Exit Sub

Command20_Click_Err:
    MsgBox Error$
    Resume Command20_Click_Exit
End Sub
 
Last edited by a moderator:

vba_php

Forum Troll
Local time
Today, 13:45
Joined
Oct 6, 2019
Messages
2,884
can you please put your code inside CODE tags? thanks! here's how you do it:

push_this_dropdown_button.jpg
 

CJ_London

Super Moderator
Staff member
Local time
Today, 19:45
Joined
Feb 19, 2013
Messages
12,450
suggest turn off your error checking for now (comment out the code) then step through the code as it runs to check the values so you can identify which line you get the error.

Also, strongly recommended to use the code tags as already advised otherwise your code is pretty unreadable to those not familiar with it

it is likely that this line is where the error is since vbString has a value of 8 so you are comparing text with a number

If Dir(strname, vbNormal) = vbString Then

perhaps try

If Dir(strname, vbNormal) <>"" Then
 

The_Doc_Man

Immoderate Moderator, Former MVP, Retired SysAdmin
Staff member
Local time
Today, 13:45
Joined
Feb 28, 2001
Messages
17,719
I have to agree with CJ's suggestion. It would help to know which line is throwing the error. However, looking over what you wrote, CJ's suggestion is probably a good guess. Watch out for intermixing vbXXX constants in your code. Not that you should NOT use the constants, but be aware that they look like a string (since they have a name) but are probably numeric.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top Bottom