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
    
   rs.MoveFirst
 
   
   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
            
            
          
      rs.MoveNext
   Wend
   rs.Close
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?
LVL 5
bfuchsAsked:
Who is Participating?
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.

Rgonzo1971Commented:
Hi,

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

Regards
0

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:
Hi,

Got the attached error msg when executing the last line.
Untitled.png
0
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"
    Else
        strSql = strSql & ",'" & Obj & "'"
        Set Obj = Nothing
    End If
On Error GoTo 0

Open in new window

0
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.
0
bfuchsAuthor Commented:
finally worked, thank you!
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.