?
Solved

Handling Zip+4 in VBA import

Posted on 2013-01-21
6
Medium Priority
?
460 Views
Last Modified: 2013-01-27
I'm trying to import data into my DB from an Excel SS or a CSV file. Here's the code I'm using:

FilePath = "C:\Users\residents.csv"
'DoCmd.TransferSpreadsheet acLink, acSpreadsheetTypeExcel7, "TblNm_SS", FilePath, True
DoCmd.TransferText acLinkDelim, , "TblNm_SS", FilePath, True

Set InRS = DB.OpenRecordset("TblNm_SS")
Set OutRS = DB.OpenRecordset("PermTbl")

If InRS.EOF = False Then
    InRS.MoveFirst
    
    Do Until InRS.EOF = True
        
        I = 0
        With OutRS                                 'the input is an empty string write it
            .AddNew
            For I = 0 To InRS.Fields.Count - 1
                .Fields(I).Value = CStr(Nz(InRS.Fields(I).Value, ""))
            Next I
            .Update
        End With
    
        InRS.MoveNext
    Loop
End If

Open in new window


The problem is that I have a zip column that a few of the records have a few zip+4 like "44081-1234" which when viewing the linked table comes up as #Num and the properties say it is a long integer in the linked table.

The data set is small enough that I can just copy and paste to the staging table. But does anyone have any suggestions to get around this if I want to automate the import process?
0
Comment
Question by:Jim P.
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
6 Comments
 
LVL 11

Expert Comment

by:ScriptAddict
ID: 38802515
Filter the table first to remove the -.  so it's only 440811234.  Or built it into the import that if the length of the zip field is a +4 then you pull that field apart before importing it.

Alternatively you could adjust the table to not have an issue with the - by changing it to a text field.  

-SA
0
 
LVL 38

Author Comment

by:Jim P.
ID: 38802535
The destination table in Access is already setup as a text field. The issue is that the VBA wants to handle the attached file as a long integer and or just an integer so I'm getting a Numeric Overflow error.
0
 
LVL 96

Expert Comment

by:Lee W, MVP
ID: 38802536
Few options:

.Fields(I).Value = CStr(Nz(InRS.Fields(I).Value, ""))

Determine which field number it is and when you get to it, simply use REPLACE.  

Example, if the Zip Code field is the 6th field, then:

If i = 6 Then
   .Fields(I).Value = Replace(CStr(Nz(InRS.Fields(I).Value, "")), "-", "")
Else
   .Fields(I).Value = CStr(Nz(InRS.Fields(I).Value, ""))
End If

Or open the source data and do a replace on that field in excel.
0
 
LVL 38

Author Comment

by:Jim P.
ID: 38803600
leew,

Still doesn't work. My error is "Numeric field overflow." because it is still trying to handle it as in integer.
0
 
LVL 38

Accepted Solution

by:
Jim P. earned 0 total points
ID: 38803611
Came up with a way around it. Handle it as a variant.

FileNum = FreeFile()

Open FilePath For Input Access Read Shared As #FileNum

Set RS = DB.OpenRecordset(TblName)    '<-- Change to your tablename

Line Input #FileNum, InputString     '<--- To throw away the header line
Do Until EOF(FileNum) = True
    Line Input #FileNum, InputString
    
    If Len(Trim(InputString)) = 0 And EOF(FileNum) = False Then
        Line Input #FileNum, InputString
    ElseIf EOF(FileNum) = True Then
        Exit Do
    End If
    
    TxtArray = Split(InputString, ",", -1, vbBinaryCompare)
    
    I = 0
    With RS                                 'the input is an empty string write it
        .AddNew
        For I = 0 To UBound(TxtArray) - 1
            .Fields(I).Value = Trim(TxtArray(I))
        Next I
        .Update
    End With
    I = 0
Loop

RS.Close
Close #FileNum

Open in new window


I do have to handle the a small problem when I save it from Excel to the CSV format. The original data is saved as Full Name "Smith, John D." and when exported from Excel that column is surrounded by quote marks. But that is easy enough to fix. I was going to have to break up the name anyway for normalization export to the the other system.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question