vendredi 17 juin 2016

VBA Access: Files with Nulls

Overall Goal: Pull all files from folder > format files in staging table > copy staging table to master table > kill staging table > rinse and repeat until all files have been taken from folder, formatted and put into the master table.

Issue: I have apparently not taken into account that some of the files sent to me will have blank worksheets (rather they may have a value that says "No Data" in cell A1). When my macro hits the "No Data" or blank sheet I get a Null error (94).

What I've tried:

  • strF1Data = Nz(!ref_val)
  • strF1Data = Nz(!ref_val,"")

Suspicions: I think I can update the SQL UPDATE line to allow Nulls, but I feel like a more efficient solution would be to skip if null. However I have tried modifying the Do Until statement and had no luck...

Possibly Worth Mentioning: The files have multiple worksheets. I learned this the hard way in finding this error on a random worksheet between several other worksheets that did have data.

Code: (to help save some space, I'm only giving the call files bit and formatting piece, I don't think the other pieces will be of any use. However if you would like them then let me know.)

The overall macro (see next code sections for piece with error):

Sub Pull_File_into_Staging_Table()
'Process:
    '1 - Loop through all files saved to specified folder making an internal list of the files
    '2 - Paste one files content to staging table at a time
    '3 - Format the information in the staging table
    '4 - Copy formatted staging table to 1Compare Table (master table)

Dim strFile As String 'Filename
Dim strFileList() As String 'File Array
Dim intFile As Integer 'File Number
Dim filename As String
Dim path As String
DoCmd.SetWarnings False
path = "C:UsersUSERDesktopTest"
Dim rs As DAO.Recordset ' Moved from below
Dim db As DAO.Database
Set db = CurrentDb

'Loop through the folder & build file list
strFile = Dir(path & "*.xls")
While strFile <> ""
'add files to the list
intFile = intFile + 1
ReDim Preserve strFileList(1 To intFile)
strFileList(intFile) = strFile
strFile = Dir()
Wend

'see if any files were found
If intFile = 0 Then
MsgBox "No files found"
Exit Sub
End If

'cycle through the list of files
For intFile = 1 To UBound(strFileList)
filename = path & strFileList(intFile)
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel8, "Stage", filename, False

Call Format_Staging_Table
Call Copy_from_Stage_to_Master
Call Clear_Staging_Table

Next intFile
DoCmd.SetWarnings True
End Sub

The piece with the issue:

Sub Format_Staging_Table()

Dim strFile As String 'Filename
Dim strFileList() As String 'File Array
Dim intFile As Integer 'File Number
Dim filename As String
Dim path As String
DoCmd.SetWarnings False
path = "C:UsersUSERDesktopTest"
Dim rs As DAO.Recordset ' Moved from below
Dim db As DAO.Database
Set db = CurrentDb

CurrentDb.Execute ("ALTER TABLE Stage ADD COLUMN UPC Text, SR_Profit_Center Text, SR_Super_Label Text, SAP_Profit_Center Text, SAP_Super_Label Text;")

CurrentDb.TableDefs("Stage").Fields("F1").Name = "ref_val"


Dim ref_val As String
Set rs = db.OpenRecordset("SELECT TOP 1 ref_val FROM Stage;", dbOpenDynaset)
ref_val = rs.Fields(0).Value
rs.Close

db.Execute "DELETE FROM [Stage] WHERE ref_val = '" & ref_val & "';"

Const YOUR_TABLE_NAME   As String = "Stage"
Dim SQL_UPDATE_DATA   As String
SQL_UPDATE_DATA = "SELECT *, ';' & '" & ref_val & "' FROM [" & YOUR_TABLE_NAME & "] WHERE SR_Profit_Center Is Null"

Dim strF1Data   As String
Dim varData     As Variant

Set rs = CurrentDb.OpenRecordset(SQL_UPDATE_DATA)
With rs
    Do Until .EOF
        strF1Data = !ref_val
        varData = Split(strF1Data, ";")
        If UBound(varData) = 4 Then
            .Edit
            !ref_val = ref_val
            !UPC = varData(0)
            !SR_Profit_Center = varData(1)
            !SR_Super_Label = varData(2)
            !SAP_Profit_Center = varData(3)
            !SAP_Super_Label = varData(4)
            .Update
        End If
        .MoveNext
    Loop
    .Close
End With

Set rs = Nothing

End Sub

Also I'm aware of the extra variable pieces, I will clean it up once I get it working.

File Examples:

Working File:

  1. CE16041901
  2. 00791558441123;US1K100017;CGR;US1K100001;UNKNOW
  3. 00791558442328;US1K100017;CGR;US1K100001;UNKNOW
  4. 00791558440720;US1K100017;CGR;US1K100001;UNKNOW
  5. 00791558444629;US1K100017;CGR;US1K100001;UNKNOW
  6. 00791558440522;US1K100017;CGR;US1K100001;UNKNOW
  7. 00791558443325;US1K100017;CGR;US1K100001;UNKNOW

Not Working File:

  1. CE16042001
  2. 00791558334128;US1K100017;CGR;US1K100001;UNKNOW
  3. 00791558159523;US1K100017;CGR;US1K100001;UNKNOW
  4. 00602547736604;US1A100018;UR;US1A100018;US-RU

I appreciate any help. I ran with this as far as I could, but I am still very much a novice when it comes to access and vb. If you need more information or clarification please let me know and I'll do my best to provide/explain.

Aucun commentaire:

Enregistrer un commentaire