VBA code to pass null values to excel

chaostheory

Registered User.
Local time
Today, 11:32
Joined
Sep 30, 2008
Messages
69
I'm at a total loss on how to do this. I have a spreadsheet with 7 fields that may or may not be filled in. The first thing the macro does is read in all 7 fields. when a field is blank i assign it to null. In the database when i run a query on the table to get the result i want, it assigns (Is Null) to empty fields which is why i assigned Null to the variants in excel if they were blank (""). But in Excel when i assign the value to Null and pass it to access it either passes the text of "Is Null" or simply doesn't run the SQL statement. I stink at SQL writing, let alone trying to write it in VBA which is even harder for me. Anyone got a solution?

The only thing i can think to do is write a separate SQL statement for each case which is what i may end up having to do, because i dont know how to write an SQL statement in excel that will allow you to pass a string OR a null depending on what the variant is. Here is the code.

Code:
Sub pullGenealogy()
Dim x As Long
Dim varConn As String, varSQL As String
Dim firstFire As Variant, extrusion As Variant, slice As Variant
Dim grind As Variant, plug As Variant, secondFire As Variant
Dim contour As Variant, skin As Variant
varConn = "ODBC; DSN=MS Access Database;DBQ=Z:\_AC_LBP\DatabaseII\AC Raw Data.mdb; Driver={Driver do Microsoft Access (*.mdb)}"
For x = 0 To ActiveSheet.UsedRange.Rows.Count
    If Range("BB6").Offset(0, 0) = "" Then
        MsgBox "There is no data to look up. Please fill in at least one row of data before proceeding."
        Exit Sub
    Else
        extrusion = Range("BB6").Offset(x, 0)
        If extrusion = "" Then
            Exit For
        End If
        
        firstFire = Range("BB6").Offset(x, 1)
        If firstFire = "" Then
            firefire = Null
        End If
        
        slice = Range("BB6").Offset(x, 2)
        If slice = "" Then
            slice = Null
        End If
        
        grind = Range("BB6").Offset(x, 3)
        If grind = "" Then
            grind = Null
        End If
        
        plug = Range("BB6").Offset(x, 4)
        If plug = "" Then
            plug = Null
        End If
        
        secondFire = Range("BB6").Offset(x, 5)
        If secondFire = "" Then
            secondifre = Null
        End If
        
        contour = Range("BB6").Offset(x, 6)
        If contour = "" Then
            contour = Null
        End If
        
        skin = Range("BB6").Offset(x, 7)
        If skin = "" Then
            skin = Null
        End If
        
        varSQL = "SELECT [Genealogy List].Genealogy " & _
        "FROM [Genealogy List] " & _
        "WHERE ((([Genealogy List].[Extrusion: Extruder Date Condition])='" & extrusion & "') AND (([Genealogy List].[1st Fire: Plant Lot])='" & firstFire & "') AND (([Genealogy List].[Slice: Request ID])='" & slice & "') " & _
        "AND (([Genealogy List].[Grind: Request ID])='" & grind & "') AND (([Genealogy List].[Plug: Request ID])='" & plug & "') AND (([Genealogy List].[2nd Fire: Plant Lot])='" & secondFire & "') " & _
        "AND (([Genealogy List].[Contour: Request ID])='" & contour & "') AND (([Genealogy List].[Skin: Request ID])='" & skin & "'));"
        MsgBox varSQL
        '" & extrusion & "'
        
        With ActiveSheet.QueryTables.Add(Connection:=varConn, Destination:=Range("BK6").Offset(x, 8))
            .CommandText = varSQL
            .Name = "Query-39008"
            .FieldNames = False
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .BackgroundQuery = True
            .RefreshStyle = xlOverwriteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = False
            .RefreshPeriod = 0
            .PreserveColumnInfo = False
            .Refresh BackgroundQuery:=False
        End With
    End If
Next x
End Sub
 

Users who are viewing this thread

Back
Top Bottom