lundi 9 décembre 2019

Access VBA if then else compare 2 tables if values are not null

I am trying to run a validation query on 2 tables, Pages and Section. The query works but if the Pages table values (equipment model and equipment serial prefix) are null then it returns any value as an error and for me, if those values are null on the Section table then they apply to everything and is not an error. I tried writing a VBA script to see if they are null then skip them else run the query. Every time I run it breaks access and I know the table sizes are not a factor. I am a noob on VBA and Access so any help would be appreciated.

Below is the script I am using.

Sub test()
''Define and set variables
    Dim db As DAO.Database
    Set db = CurrentDb()

    Dim p As Recordset, m As Recordset
    Set p = db.OpenRecordset("Pages", dbOpenDynaset)
    Set m = db.OpenRecordset("Section", dbOpenDynaset)

    'Ignores popup box and runs the query
    DoCmd.SetWarnings False



Do Until p.EOF

If Not IsNull(m![equipmentmodel]) And Not IsNull(m![equipmentserialprefix]) Then
DoCmd.RunSQL ("INSERT INTO [Pages Table Errors] ( Bookid, Category, Errors ) SELECT distinct [p.Bookid] AS Expr1, 'Section vs Pages Combinations' AS Expr2, 'Pages ID ' & p.id & ', EqModel ' & '''' & p.equipmentmodel & '''' & ', EqSerPrefix ' & '''' & p.equipmentserialprefix & '''' & ' not on Section tbl' AS Expr123 FROM Section as m RIGHT JOIN Pages as p ON (M.EQUIPMENTMODEL = P.EQUIPMENTMODEL) AND (M.BOOKID = P.BOOKID) and (m.equipmentserialprefix = p.equipmentserialprefix) WHERE p.equipmentmodel is not null and p.equipmentmodel <>'' and p.equipmentserialprefix is not null and p.equipmentserialprefix <>'' and M.EQUIPMENTmodel Is Null;")

Else

p.MoveNext

 End If

Loop

End Sub

Aucun commentaire:

Enregistrer un commentaire