Solved

Handling Zip+4 in VBA import

Posted on 2013-01-21
6
459 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

688 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