Wits end trying to get VBA code to work (1 Viewer)

Locopete99

Registered User.
Local time
Today, 13:14
Joined
Jul 11, 2016
Messages
163
Hi,

Can someone help me. I cant get this code to work!

Literally all i want it to do is run different export queries based on what area the person running it is from.

Can anyone see where the glaring issues are that are stopping this from running.

Code:
Dim Holon1 As String
Dim Location As String
Dim DirName As String
Dim Response As String
DirName = "B:\" & fOSUserName & "\Register Report\Outstanding Emails\"
If Dir(DirName, vbDirectory) = "" Then
MkDir DirName
Else
End If

Holon1 = DLookup("Holon", "tbl_Holonsecurity", "[User]= '" & fOSUserName() & "'")

If Holon1 = "AA" Then
file = "AA.xlsx"
strpath = DirName & file
DoCmd.TransferSpreadsheet acExport, 9, "Qry_OutHolonAA", strpath, True
Else

If Holon1 = "AB" Then
file = "AB.xlsx"
strpath = DirName & file
DoCmd.TransferSpreadsheet acExport, 9, "Qry_OutHolonAB", strpath, True
Else

If Holon1 = "AC" Then
file = "AC.xlsx"
strpath = DirName & file
DoCmd.TransferSpreadsheet acExport, 9, "Qry_OutHolonAC", strpath, True
Else

If Holon1 = "CDT" Then
file = "CDT.xlsx"
strpath = DirName & file
DoCmd.TransferSpreadsheet acExport, 9, "Qry_OutHolonCDT", strpath, True
Else

If Holon1 = "A0" Then
file = "AA.xlsx"
file2 = "AB.xlsx"
file3 = "AC.xlsx"
file4 = "CDT.xlsx"
strpath = DirName & file
strpath2 = DirName & file2
strpath3 = DirName & file3
strpath4 = DirName & file4
DoCmd.TransferSpreadsheet acExport, 9, "Qry_OutHolonAA", strpath, True
DoCmd.TransferSpreadsheet acExport, 9, "Qry_OutHolonAB", strpath2, True
DoCmd.TransferSpreadsheet acExport, 9, "Qry_OutHolonAC", strpath3, True
DoCmd.TransferSpreadsheet acExport, 9, "Qry_OutHolonCDT", strpath4, True
Else
End If
End If
End If
End If
End If
 

Gasman

Enthusiastic Amateur
Local time
Today, 20:14
Joined
Sep 21, 2011
Messages
14,047
First question would be, what is it not doing?
Why not walk through the code with F8 ?

Code:
Sub Nothing1()


    Dim Holon1 As String
    Dim Location As String
    Dim DirName As String
    Dim Response As String
    DirName = "B:" & fOSUserName & "\Register Report\Outstanding Emails"
    If Dir(DirName, vbDirectory) = "" Then
        MkDir DirName
    Else
    End If

    Holon1 = DLookup("Holon", "tbl_Holonsecurity", "[User]= '" & fOSUserName() & "'")

    If Holon1 = "AA" Then
        File = "AA.xlsx"
        strPath = DirName & File
        DoCmd.TransferSpreadsheet acExport, 9, "Qry_OutHolonAA", strPath, True
    Else

        If Holon1 = "AB" Then
            File = "AB.xlsx"
            strPath = DirName & File
            DoCmd.TransferSpreadsheet acExport, 9, "Qry_OutHolonAB", strPath, True
        Else

            If Holon1 = "AC" Then
                File = "AC.xlsx"
                strPath = DirName & File
                DoCmd.TransferSpreadsheet acExport, 9, "Qry_OutHolonAC", strPath, True
            Else

                If Holon1 = "CDT" Then
                    File = "CDT.xlsx"
                    strPath = DirName & File
                    DoCmd.TransferSpreadsheet acExport, 9, "Qry_OutHolonCDT", strPath, True
                Else

                    If Holon1 = "A0" Then
                        File = "AA.xlsx"
                        file2 = "AB.xlsx"
                        file3 = "AC.xlsx"
                        file4 = "CDT.xlsx"
                        strPath = DirName & File
                        strpath2 = DirName & file2
                        strpath3 = DirName & file3
                        strpath4 = DirName & file4
                        DoCmd.TransferSpreadsheet acExport, 9, "Qry_OutHolonAA", strPath, True
                        DoCmd.TransferSpreadsheet acExport, 9, "Qry_OutHolonAB", strpath2, True
                        DoCmd.TransferSpreadsheet acExport, 9, "Qry_OutHolonAC", strpath3, True
                        DoCmd.TransferSpreadsheet acExport, 9, "Qry_OutHolonCDT", strpath4, True
                    Else
                    End If
                End If
            End If
        End If
    End If

End Sub
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 13:14
Joined
Aug 30, 2003
Messages
36,118
What exactly goes wrong? Where is the code (what event)? Have you set a breakpoint to see if the correct value is pulled with the DLookup?
 

Locopete99

Registered User.
Local time
Today, 13:14
Joined
Jul 11, 2016
Messages
163
Hi Gasman,
Its not doing anything!

Its supposed to create a directory and then run an export.

plus my f8 has never done anything, so that'd help if i knew how to fix that too
 

Locopete99

Registered User.
Local time
Today, 13:14
Joined
Jul 11, 2016
Messages
163
Pbaldy,

I've used a msgbox to confirm that its pulling back the right data, but then its not running any of the exports or creating the directory.
 

Gasman

Enthusiastic Amateur
Local time
Today, 20:14
Joined
Sep 21, 2011
Messages
14,047
Well the dirname will probably need to be
Code:
 "B:\" to start with?
Site removes backslash when not in code tags :(

I'm not sure you can create sub folders if the parent is not created.?

You need to set a breakpoint before you can use F8 ?
 

Users who are viewing this thread

Top Bottom