Solved

Handling Zip+4 in VBA import

Posted on 2013-01-21
6
443 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.
  • 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 95

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
update query 4 25
File.Search issue 8 32
Record with #Error 8 33
The User Would Like to Select Multiple PDF Files at Once from The File Dialog Box 7 31
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

856 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