Option Compare Database
Option Explicit
Public Sub SetupUsableData()
Dim db As DAO.Database
Dim ImportData As DAO.Recordset
Dim UsableData As DAO.Recordset
Dim sqlCode As String
Dim PayorCode As String
Dim PayorName As String
Dim lngRecordCount As Long
' Warnings are disabled so the user doesn't have to go through the process of clicking yes several times.
DoCmd.SetWarnings False
Set db = CurrentDb()
' Generating the code to import the data file and get the data I needed was tricky. In the end I found a way,
' but it is a little messy to look at. I ended up having to break up the first few columns of the data file in order
' several times to properly separate all the data I needed. I then piece the code back together based on the way it is
' in tblImportData. It is inserted into tblUsableData. If you want to look at how the data is broken up, open
' tblImportData or the specification file from File -> Get External Data -> Import -> Advanced.
' The import method is specifically designed to work with the detail file in its setup as of 7/21/08. If any
' modification is made to the detail file's layout, even the slightest change, the import method probably will no longer
' work.
sqlCode = "INSERT INTO tblUsableData ( SRCol," & _
"Client_Code, Client_Name, Tran_ID, Inbnd_Bytes, Otbnd_Bytes, Bytes, Rate, Amount, RCCol, MTCol, MCCol, IOCol, ASCD_Client, " & _
"Billable_Bytes, ECol ) SELECT tblImportData.PSR, " & _
"IIf([PN1CC2] Is Null,[CC1],[CC1]+[PN1CC2]) AS Expr1, IIf([PC1CN2] Is Null,[PN2CN1],[PN2CN1]+[PC1CN2]) AS Expr2, " & _
"tblImportData.PC2TRID, tblImportData.INBND_BYTES, tblImportData.OTBND_BYTES, tblImportData.BYTES, tblImportData.RATE, tblImportData.AMOUNT, " & _
"tblImportData.RCCol, tblImportData.MTCol, tblImportData.MCCol, tblImportData.IOCol, tblImportData.ASCD_CLIENT, " & _
"tblImportData.BILLABLE_BYTES, tblImportData.ECOL FROM tblImportData " & _
"WHERE tblImportData.BILLABLE_BYTES Is Not Null;"
DoCmd.RunSQL sqlCode
' Open only entries where the payor_code is null will ensure that only the most recent imported data is opened because
' all other data should have a papyor code.
Set UsableData = db.OpenRecordset("SELECT * FROM tblUsableData WHERE Payor_Code Is Null")
Set ImportData = CurrentDb.OpenRecordset("SELECT * FROM tblImportData")
' The following while statement runs until the end of tblImportData. The code here looks for data in tblImportData's
' first column that matches "PA", then get the payor code from the next two columns. This payor_code is applied to
' rows in tblUsableData until "TOTAL" is reached in tblImportData. At that point, "TOTAL" is inserted into the
' Client_Name portion of tblUsableData, and the payor_code and payor_name are applied for the last time in
' tblUsableData. The process then loops and a new payor_code is gathered, starting the process over again.
Do While Not ImportData.EOF
If (ImportData.Fields("PSR").Value = "PA") Then
If (ImportData.Fields("PC2TRID").Value <> "") Then
PayorCode = (ImportData.Fields("PC1CN2").Value + ImportData.Fields("PC2TRID").Value)
If (ImportData.Fields("PN2CN1").Value <> "") Then
PayorName = (ImportData.Fields("PN1CC2").Value + ImportData.Fields("PN2CN1"))
Else
PayorName = ImportData.Fields("PN1CC2").Value
End If
ImportData.MoveNext
Else
PayorCode = ImportData.Fields("PC1CN2").Value
If (ImportData.Fields("PN2CN1").Value <> "") Then
PayorName = (ImportData.Fields("PN1CC2").Value + ImportData.Fields("PN2CN1"))
Else
PayorName = ImportData.Fields("PN1CC2").Value
End If
ImportData.MoveNext
End If
Do Until ImportData.Fields("PN1CC2").Value = " TOTAL"
If ((ImportData.Fields("PSR").Value = "R" Or ImportData.Fields("PSR").Value = "S") And ImportData.Fields("BILLABLE_BYTES") <> "") Then
UsableData.Edit
UsableData.Fields("Payor_Code").Properties("Value") = PayorCode
UsableData.Fields("Payor_Name").Properties("Value") = PayorName
UsableData.Update
UsableData.MoveNext
End If
ImportData.MoveNext
Loop
If (IsNull(UsableData.Fields("Client_Code").Value)) And IsNull(UsableData.Fields("Rate").Value) Then
UsableData.Edit
UsableData.Fields("Payor_Code").Properties("Value") = PayorCode
UsableData.Fields("Payor_Name").Properties("Value") = PayorName
UsableData.Fields("Client_Name").Value = "TOTAL"
UsableData.Update
UsableData.MoveNext
End If
End If
ImportData.MoveNext
Loop
' Once completed, all data is removed from tblImportData to conserve space
DoCmd.RunSQL "DELETE FROM tblImportData;"
DoCmd.SetWarnings True
MsgBox "Import Complete!"
End Sub