How to handle missing fields in CSV file from VBA?

Hi Experts,
I have the following code to import records from an Excel file into my SQL table using MS Access ADP.
Public Sub ImportEmployeeFromWebApp()
   Dim conn As New ADODB.Connection
   Dim rs As New ADODB.Recordset
   Dim f As ADODB.Field
   Dim v As Variant
   Dim strSql As String
   conn.Open "DRIVER={Microsoft Text Driver (*.txt; *.csv)};DBQ=C:\Application;"
   rs.Open "SELECT * FROM [Application.csv]", conn, adOpenStatic, adLockReadOnly, adCmdText
   While Not rs.EOF
            strSql = "Insert into Employeestbl" _
            & "(LastName,FirstName,MiddleInitial,Title,AddressLine1,City,State,Zip,HomePhone,Beeper,WorkPhone,Extension,Phone2,Email,BestTimeToReach,AvailibilityPDays,Resume,Resume2 )" _
            & " Values (" & ConvertEmptyStrToNull(rs.Fields("LastName")) & "," & ConvertEmptyStrToNull(rs.Fields("FirstName")) & "," & ConvertEmptyStrToNull(rs.Fields("MiddleInitial")) & ",'" & rs.Fields("Title") & "'," & ConvertEmptyStrToNull(rs.Fields("AddressLine1")) & "," & ConvertEmptyStrToNull(rs.Fields("City")) & "," & ConvertEmptyStrToNull(rs.Fields("State")) & "," _
            & ConvertEmptyStrToNull(rs.Fields("Zip")) & "," & ConvertEmptyStrToNull(rs.Fields("HomePhone")) & "," & ConvertEmptyStrToNull(rs.Fields("Beeper")) & "," & ConvertEmptyStrToNull(rs.Fields("WorkPhone")) & "," & ConvertEmptyStrToNull(rs.Fields("Extension")) & "," _
            & ConvertEmptyStrToNull(rs.Fields("Phone2")) & "," & ConvertEmptyStrToNull(rs.Fields("Email")) & "," & ConvertEmptyStrToNull(rs.Fields("BestTime")) & "," & ConvertEmptyStrToNull(rs.Fields("AssignmentInterest")) & "," & ConvertEmptyStrToNull(rs.Fields("HearAbout")) & "," _
            & ConvertEmptyStrToNull(rs.Fields("OtherHearAbout")) & ")"
            CurrentProject.Connection.Execute strSql
End Sub

Open in new window

Now my question is, how do I program to check for the existence of a field while building the dynamic SQL string, it should not result in an error in case some fields are missing in that CSV file?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


pls try

On Error Resume Next
Set obj = rs.Fields("Email")
On Error GoTo 0
Str = Str & IIf(IsEmpty(obj), "", "," & ConvertEmptyStrToNull(rs.Fields("Email")))

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bfuchsAuthor Commented:

Got the attached error msg when executing the last line.
bfuchsAuthor Commented:
I'm trying the following, dont get an error but isempty is never true.

   On Error Resume Next
   sFld = "LastName"
   Set Obj = rs.Fields("LastName")
    If IsEmpty(Obj) Or IsNull(Obj) Then
        strSql = strSql & ",Null"
        strSql = strSql & ",'" & Obj & "'"
        Set Obj = Nothing
    End If
On Error GoTo 0

Open in new window

bfuchsAuthor Commented:
Added If IsEmpty(Obj) Or IsNull(Obj)  or Len(Obj)=0 Then.. and seems to work, will do more testing & let u know.
bfuchsAuthor Commented:
finally worked, thank you!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.