Import Complex info from Excel via VBA

C

C Hayes

I currently have a piece of vba that exports information from an Excel
spreadsheet into an Access db. It's complex, places info in different tables
as neccessary, loops through each record of the spreadsheet, checks for
duplicates, etcetera.

This all occurs in Excel, I want to have this all happen in Access. My goal
is to have a pop up file locator, locate the sheet to import and then do the
work in Access.

I've encluded the vba to show you what is occuring:

Private Sub FirstGiving()
Dim rsID As Long
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim rsDonors As DAO.Recordset
Dim rsAddresses As DAO.Recordset
Dim rsContributions As DAO.Recordset
Dim rsIdentities As DAO.Recordset


Dim strSQL As String
Dim blnFound As Boolean

' strings for data on spreadsheet
Dim strName As String
Dim strWholeAddress As String
Dim strEmail As String

' strings for database
Dim strFirstName As String
Dim strMiddleName As String
Dim strLastName As String
Dim strAddress As String
Dim strCity As String
Dim strState As String
Dim strZip As String
' email address can go just as it is on the spreadsheet

' variables for contribution
Dim strCode As String
Dim lngKey As Long
Dim strDescription As String
Dim strCaptain As String
Dim strContributionDate As String
Dim dtContributionDate As Date
Dim curAmount As Currency
Dim lngGLCode As Long
Dim strGLCode As String
Const strCheck As String = "CC"
Dim strYear As String
Dim strBatch As String
strContributionDate = Range("A3")
strContributionDate = Right(strContributionDate, Len(strContributionDate) -
15)
strDescription = InputBox("Which Walk is this for?", "Which Walk?",
"Nashville")
strDescription = strDescription & " Walk - "

'capture the GL Code from the user
Do While lngGLCode = 0
strGLCode = InputBox("Please enter the proper GL Code" & vbCrLf & _
"55100 for Nashville" & vbCrLf & _
"55200 for Murfreesboro" & vbCrLf & _
"55300 for Clarksville" & vbCrLf & _
"55400 for Columbia" & vbCrLf & _
"55500 for Hendersonville" & vbCrLf & _
"51200 for Transplant Games", "Select GL Code")
lngGLCode = Val(strGLCode)
If lngGLCode = 0 Then
MsgBox "You entered an invalid GL Code" & vbCrLf & _
"Please try again.", vbOKOnly
End If
Loop

strBatch = CheckBatch(strContributionDate)
dtContributionDate = FormatDateTime(strContributionDate, vbShortDate)

'integers for calculating the first and second space in a name
Dim intFS As Integer
Dim intSS As Integer


'start the loop through the records on the sheet
Dim i As Integer
'initiate i as the first record on a normal firstgiving report (the fifth row)
i = 5

Set db = OpenDatabase("C:\Contributions_FrontEnd.mdb")
Do
'name = the 'f' column and the row number
strName = Range("H" & i)

If strName = "" Then Exit Do

'break the name apart
'calculate first and second space location
intFS = InStr(1, strName, " ") - 1
intSS = InStr(intFS + 2, strName, " ")

strFirstName = Left(strName, intFS)

If intSS <> 0 Then
strLastName = Trim(Right(strName, Len(strName) - (intSS)))
Else
strLastName = Trim(Right(strName, Len(strName) - (intFS)))
End If



'if there is a second space there is a middle initial, capture the middle
initial
If intSS <> 0 Then
strMiddleName = Mid(strName, intFS + 2, 1)
End If

'address = the 'G' column and the row number
strWholeAddress = Trim(Range("I" & i))
intFS = InStr(1, strWholeAddress, " ")
If intFS = 0 Then
strWholeAddress = InputBox("I need help telling the difference" & vbCrLf & _
"between the ADDRESS LINE and the CITY STATE ZIP" & vbCrLf & vbCrLf & _
"Please add extra spaces (2 or 3)" & _
vbCrLf & _
"between the ADDRESS LINE and the CITY STATE ZIP", _
"Add spaces to help delimit", strWholeAddress)
intFS = InStr(1, strWholeAddress, " ")
End If

'capture the first address line and then eliminate it from strWholeAddress
strAddress = Left(strWholeAddress, intFS)
strWholeAddress = Trim(Right(strWholeAddress, (Len(strWholeAddress) - intFS)))

'capture the City
strCity = Left(strWholeAddress, Len(strWholeAddress) - 9)

'capture the state and zip
strZip = Right(strWholeAddress, 5)
strState = Mid(strWholeAddress, Len(strWholeAddress) - 7, 2)

'capture email
strEmail = Range("J" & i)

'check for duplicate entries in database

'create a sql string that pulls identical records
strSQL = "SELECT * " & _
"FROM qryDonorAddresses " & _
"WHERE LastName Like '" & Left(strLastName, 3) & "*' " & _
"AND Address1 Like '" & Left(strAddress, 5) & "*'"

'set the recordset to such
Set rs = db.OpenRecordset(strSQL)
Set rsDonors = db.OpenRecordset("tblDonors")
Set rsAddresses = db.OpenRecordset("tblAddresses")

'loop through the records (if there are any) and inform the user
Do Until rs.EOF

'if the address is obviously the same (10 characters) then don't inform the
user
If Left(strAddress, 10) = Left(rs.Fields("Address1"), 10) Then
blnFound = True
rsID = rs![tblDonors.DonorID]
Exit Do
End If

If MsgBox("This record exists:" & vbCrLf & _
rs.Fields("FirstName") & " " & rs.Fields("LastName") & vbCrLf & _
rs.Fields("Address1") & vbCrLf & vbCrLf & _
"Your Record is:" & vbCrLf & _
strFirstName & " " & strLastName & vbCrLf & _
strAddress & vbCrLf & vbCrLf & _
"Is this the same person?", vbYesNo + vbQuestion) = vbYes Then

'if a record IS found then update the email ONLY
'if blnFound is true then the AddNew will not occur
blnFound = True
rs.Edit
rs!Emailaddress = strEmail
rs.Update
Exit Do
End If
rs.MoveNext
Loop
Debug.Print i
Debug.Print strFirstName
Debug.Print strMiddleName
Debug.Print strLastName
Debug.Print strAddress
Debug.Print strCity
Debug.Print strState
Debug.Print strZip
Debug.Print strEmail




'if no record was found then add the record to the db
If blnFound = False Then
With rsDonors
.AddNew
rsDonors!FirstName = strFirstName
rsDonors!LastName = strLastName
rsDonors!MiddleName = strMiddleName
rsDonors!Emailaddress = strEmail
.Update
End With

rsDonors.Bookmark = rsDonors.LastModified
rsID = rsDonors!DonorID

With rsAddresses
.AddNew
rsAddresses!DonorID = rsID
rsAddresses!Address1 = strAddress
rsAddresses!City = strCity
rsAddresses!State = strState
rsAddresses!Zip = strZip
.Update
End With
End If

'add the contribution record to the db

strDescription = strDescription & Range("D" & i)
strDescription = Left(strDescription, Len(strDescription) - 7)

lngKey = rsID
strSQL = "SELECT * FROM tblContributions"
Set rsContributions = db.OpenRecordset(strSQL)
With rsContributions
.AddNew
rsContributions!DonorID = lngKey
rsContributions!Date = dtContributionDate
rsContributions!Description = strDescription
rsContributions!Amount = Range("L" & i)
rsContributions!GLCode = lngGLCode
rsContributions!Batch = strBatch
rsContributions!CheckNumber = strCheck
rsContributions!EventYear = Right(strContributionDate, 4)
.Update
End With

rsContributions.Bookmark = rsContributions.LastModified

rsID = rsContributions!ContributionID

Select Case lngGLCode
Case 55100, 55200, 55300, 55400, 55500
strCode = "WLK-SPONS"
Case 51200
strCode = "TRANSPLANT"
End Select

Set rsIdentities = db.OpenRecordset("tblContributionIdentities")
With rsIdentities
.AddNew
rsIdentities!ContributionID = rsID
rsIdentities!ContributionCode = strCode
.Update
End With


'reset description string
intFS = InStr(1, strDescription, "-")
intFS = Len(strDescription) - intFS

strDescription = Left(strDescription, Len(strDescription) - intFS + 1)

'zero out capture strings
strName = ""
strWholeAddress = ""

'blnFound to false RESET
blnFound = False

'increment to next row
i = i + 1
Loop

'zero out db variables
Set db = Nothing
Set rs = Nothing
Set rsDonors = Nothing
Set rsAddresses = Nothing
Set rsContributions = Nothing
Set rsIdentities = Nothing
End Sub

Private Function CheckBatch(strCD As String) As String
Dim db As DAO.Database
Dim rs As DAO.Recordset
'Used for checking on open batch numbers
'date for filtering SQL
Dim dtCheckDate As Date

'making date a string for SQL
dtCheckDate = FormatDateTime(strCD, vbShortDate)

'SQL for recordset
Dim strSQL As String

strSQL = "SELECT tblContributions.Batch, " & _
"tblContributions.Date " & _
"FROM tblContributions GROUP BY tblContributions.Batch, " & _
"tblContributions.Date " & _
"HAVING tblContributions.Date = " & Chr(35) & _
dtCheckDate & Chr(35) & " ORDER BY tblContributions.Batch;"

'record set variable
Set db = OpenDatabase("C:\Contributions_FrontEnd.mdb")
Set rs = db.OpenRecordset(strSQL)

'create a batch date style date MMDDYY
Dim strBDMonth As String
Dim strBDDay As String
Dim strBDYear As String
strBDYear = Right(strCD, 2)
Dim intFM As Integer
Dim intSM As Integer
intFM = InStr(1, strCD, "/")
intSM = InStr(intFM + 1, strCD, "/")
If intFM = 2 Then
strBDMonth = "0" & Left(strCD, 1)
Else
strBDMonth = Left(strCD, 2)
End If
If (intFM - intSM) = 2 Then
strBDDay = "0" & Mid(strCD, intFM + 1, 1)
Else
strBDDay = Mid(strCD, intFM + 1, 2)
End If

'check for records
'if none than let the user know and kick out of code
If rs.EOF Then
CheckBatch = "FG" & strBDMonth & _
strBDDay & strBDYear & "01"
Set db = Nothing
Set rs = Nothing
Exit Function
End If

'if there are records, loop through and build the correct/highest sequence
Dim strSequence As String
Do While Not rs.EOF
If Val(Right(rs.Fields(0), 1)) > Val(strSequence) Then
strSequence = Val(Right(rs.Fields(0), 1))
rs.MoveNext
Else
rs.MoveNext
End If
Loop
CheckBatch = "FG" & strBDMonth & _
strBDDay & strBDYear & "0" & Val(strSequence) + 1
Set db = Nothing
Set rs = Nothing

End Function
 
J

Jeanette Cunningham

In access we call it automating excel.
You can open the excel file, do all the checking of the info on the excel
sheet using automation from access.
After finished checking you can use the TransferSpreadsheet method to import
the data into the tables.

I have some code that shows how to open and manipulate an access file from
access.
You will need to set the references to all the Excel objects in a similar
way to the way my code does.
Note how I reference and call the excel objects when I want to manipulate
them.


You will need to download code from The Access Web,
http://www.mvps.org/access/api/api0007.htm
copy the code above into a separate class module and give it a name like
modFindRunningApp andsave

that is the code used by my code on this line
If fIsAppRunning("excel", False) Then 'yes it is running


To get this code below working for you,
--put a button a form
--Code its Click event like this
----------------------------------------------------
Private Sub ExportBtn_Click()
Call FormatXLReport(strPath, _
strFile, _
strMakeActive)

'strMakeActive name of worksheet to activate
'**Note: You must supply the values for strPath, strFile, strMakeActive

End Sub
-----------------------------------------------------


After you are successful with opening the excel file, checking and importing
the data,
post back and we can show you how to code so that user can select the file
from the common open save windows dialog.


----------------------------------------------------
Public Sub FormatXLReport(strPath As String, _
strFile As String, _
strMakeActive As String)

'strMakeActive name of worksheet to activate

On Error GoTo SubErr
pstrProc = "FormatXLReport"
pstrSubProc = "FormatXLReport"
Dim db As DAO.Database
Dim blnExcelExists As Boolean
Dim objXLApp As Object
Dim objActiveWkb As Object
Dim objXLWkb As Object
Dim objXLws As Object
Dim strWkbName As String
Dim strCriteria As String
Dim sngColumnWidth As Single 'width of column
Dim strRange As String 'used to set the print area
Dim intI As Integer 'loop counter
Dim lngColumnCount As Long
Dim lngAlignR As Long


'Const xlMaximized As Integer = -4137
Const xlLandscape = 2
Const xlRight = -4152
Const xlCentre = -4108
Const xlAutomatic = -4105
Const xlContinuous = 1
Const xlCellTypeLastCell = 11


Set db = DBEngine(0)(0)
'Open the raw data spreadsheet for formatiing

If fIsAppRunning("excel", False) Then 'yes it is running
' Get a reference to currently running Excel window
Set objXLApp = GetObject(, "Excel.Application")
blnExcelExists = True
Else
' Excel is not currently running so create a new instance
Set objXLApp = CreateObject("Excel.Application")
End If


'Hide warnings on the spreadsheet
objXLApp.DisplayAlerts = False
'prevent any excel macros from running
objXLApp.Interactive = False
'hide screen changes
objXLApp.ScreenUpdating = False
'Open a workbook
objXLApp.Workbooks.Open (strPath)
'point to the active workbook
Set objXLWkb = objXLApp.Workbooks(strFile)
'Debug.Print "active workbook: " & objXLWkb.Name
'activate the selected workbook
objXLWkb.Activate
'Debug.Print strMakeActive
'Debug.Print "active sheet: " & ObjXLApp.ActiveWorkbook.Worksheets(1)
'point to the wanted worksheet
Set objXLws = objXLApp.ActiveWorkbook.Worksheets(1)
'activate the selected worksheet
objXLws.Activate

'now format the report

'get the count of how many columns for this report
lngColumnCount = Nz(DMax("[ColumnNo]", "tlkpXLRptProps", "[ReportID] = "
& lngReportID), 0)
lngAlignR = Nz(DLookup("[AlignR]", "tlkpXLReport", "[ReportID] = " &
lngReportID), 0)
'Debug.Print lngAlignR


'if any columns with costs then right align them
If lngAlignR > 0 Then
With objXLws.Cells
.columns(lngAlignR).horizontalalignment = xlRight
'centre the heading for the column with costs
.Range(.Cells(1, lngAlignR), .Cells(1,
lngAlignR)).horizontalalignment = xlCentre
End With
End If


With objXLws.Cells
'do column headings
For intI = 1 To lngColumnCount
.Range(.Cells(1, intI), .Cells(1, intI)) = _
FindColumnTitle(lngColumnNo:=intI, lngReportNo:=lngReportID)
Next intI


'format the cells
.Font.Name = "Arial"
.Font.FontStyle = "Regular"
.Font.Size = 10
'bold the headings
.Rows("1:1").Font.Bold = True
.WrapText = True
'set heading cell colour to white (OutputTo colours them grey)
.Range(.Cells(1, 1), .Cells(1, lngColumnCount)).Interior.Color =
RGB(255, 255, 255) 'white

'Put Borders around all cells in the Data Area
.Range(.Cells(1, 1), .Cells(1,
1).SpecialCells(xlCellTypeLastCell)).Borders.LineStyle = xlContinuous
.Range(.Cells(1, 1), .Cells(1,
1).SpecialCells(xlCellTypeLastCell)).Borders.ColorIndex = xlAutomatic

'set the column width
For intI = 1 To lngColumnCount
.Range(.Cells(1, intI), .Cells(1, intI)).ColumnWidth = _
FindColumnWidth(lngColumnNo:=intI,
lngTemplateNo:=lngReportID)
Next intI

'auto fit row height
.Rows.AutoFit

strRange = .Range(.Cells(1, 1), .Cells(1,
1).SpecialCells(xlCellTypeLastCell)).Address
'Debug.Print strRange
End With


'now do page set up

With objXLws.PageSetup
.Orientation = xlLandscape
'If zoom property is False, the FitToPagesWide and FitToPagesTall
properties
'control how the worksheet is scaled
.Zoom = False
.FitToPagesTall = False
.FitToPagesWide = 1
' .CenterHeader = Me.txtCurrYear & " " & Me.cboResource _
' & " Hours " & strMonth & " YTD"
.CenterFooter = "&F"
' .CenterFooter = "&F" & " " & "&D"
.RightFooter = "&R Page &P of &N"
.LeftMargin = objXLApp.CentimetersToPoints(0.5)
.RightMargin = objXLApp.CentimetersToPoints(1.5)
.TopMargin = objXLApp.CentimetersToPoints(1#)
.BottomMargin = objXLApp.CentimetersToPoints(1#)
.HeaderMargin = objXLApp.CentimetersToPoints(0.7)
.FooterMargin = objXLApp.CentimetersToPoints(0.7)
.printarea = strRange
'Debug.Print .printarea
End With

'put focus back to first data cell
objXLws.Range("A2").Select

'Prevent Excel from prompting to save changes
objXLApp.ActiveWorkbook.Save

SubExit:
'turn on warnings on the spreadsheet
objXLApp.DisplayAlerts = True
'allow any excel macros from running
objXLApp.Interactive = True
'show screen changes
objXLApp.ScreenUpdating = True

'close the instance of Excel created by code
If Not blnExcelExists Then
objXLApp.Quit
End If

If Not objActiveWkb Is Nothing Then
Set objActiveWkb = Nothing
End If
If Not objXLApp Is Nothing Then
Set objXLApp = Nothing
End If
If Not db Is Nothing Then
Set db = Nothing
End If

DoCmd.Hourglass False
Exit Sub

SubErr:
Select Case Err.Number
Case 3010
MsgBox pmsg2 & strPath & pmsg3, vbInformation, pstrT
Case 70, 430
MsgBox pmsg4, vbInformation, pstrT
Case Else
Call fnFormErrHandler(pstrProc, pstrSubProc, pstrMdl, Err)
End Select
Resume SubExit

End Sub
----------------------------------------------------

Jeanette Cunningham


C Hayes said:
I currently have a piece of vba that exports information from an Excel
spreadsheet into an Access db. It's complex, places info in different
tables
as neccessary, loops through each record of the spreadsheet, checks for
duplicates, etcetera.

This all occurs in Excel, I want to have this all happen in Access. My
goal
is to have a pop up file locator, locate the sheet to import and then do
the
work in Access.

I've encluded the vba to show you what is occuring:

Private Sub FirstGiving()
Dim rsID As Long
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim rsDonors As DAO.Recordset
Dim rsAddresses As DAO.Recordset
Dim rsContributions As DAO.Recordset
Dim rsIdentities As DAO.Recordset


Dim strSQL As String
Dim blnFound As Boolean

' strings for data on spreadsheet
Dim strName As String
Dim strWholeAddress As String
Dim strEmail As String

' strings for database
Dim strFirstName As String
Dim strMiddleName As String
Dim strLastName As String
Dim strAddress As String
Dim strCity As String
Dim strState As String
Dim strZip As String
' email address can go just as it is on the spreadsheet

' variables for contribution
Dim strCode As String
Dim lngKey As Long
Dim strDescription As String
Dim strCaptain As String
Dim strContributionDate As String
Dim dtContributionDate As Date
Dim curAmount As Currency
Dim lngGLCode As Long
Dim strGLCode As String
Const strCheck As String = "CC"
Dim strYear As String
Dim strBatch As String
strContributionDate = Range("A3")
strContributionDate = Right(strContributionDate,
Len(strContributionDate) -
15)
strDescription = InputBox("Which Walk is this for?", "Which Walk?",
"Nashville")
strDescription = strDescription & " Walk - "

'capture the GL Code from the user
Do While lngGLCode = 0
strGLCode = InputBox("Please enter the proper GL Code" & vbCrLf & _
"55100 for Nashville" & vbCrLf & _
"55200 for Murfreesboro" & vbCrLf & _
"55300 for Clarksville" & vbCrLf & _
"55400 for Columbia" & vbCrLf & _
"55500 for Hendersonville" & vbCrLf & _
"51200 for Transplant Games", "Select GL Code")
lngGLCode = Val(strGLCode)
If lngGLCode = 0 Then
MsgBox "You entered an invalid GL Code" & vbCrLf & _
"Please try again.", vbOKOnly
End If
Loop

strBatch = CheckBatch(strContributionDate)
dtContributionDate = FormatDateTime(strContributionDate, vbShortDate)

'integers for calculating the first and second space in a name
Dim intFS As Integer
Dim intSS As Integer


'start the loop through the records on the sheet
Dim i As Integer
'initiate i as the first record on a normal firstgiving report (the fifth
row)
i = 5

Set db = OpenDatabase("C:\Contributions_FrontEnd.mdb")
Do
'name = the 'f' column and the row number
strName = Range("H" & i)

If strName = "" Then Exit Do

'break the name apart
'calculate first and second space location
intFS = InStr(1, strName, " ") - 1
intSS = InStr(intFS + 2, strName, " ")

strFirstName = Left(strName, intFS)

If intSS <> 0 Then
strLastName = Trim(Right(strName, Len(strName) - (intSS)))
Else
strLastName = Trim(Right(strName, Len(strName) - (intFS)))
End If



'if there is a second space there is a middle initial, capture the middle
initial
If intSS <> 0 Then
strMiddleName = Mid(strName, intFS + 2, 1)
End If

'address = the 'G' column and the row number
strWholeAddress = Trim(Range("I" & i))
intFS = InStr(1, strWholeAddress, " ")
If intFS = 0 Then
strWholeAddress = InputBox("I need help telling the difference" & vbCrLf &
_
"between the ADDRESS LINE and the CITY STATE ZIP" & vbCrLf & vbCrLf & _
"Please add extra spaces (2 or 3)" & _
vbCrLf & _
"between the ADDRESS LINE and the CITY STATE ZIP", _
"Add spaces to help delimit", strWholeAddress)
intFS = InStr(1, strWholeAddress, " ")
End If

'capture the first address line and then eliminate it from strWholeAddress
strAddress = Left(strWholeAddress, intFS)
strWholeAddress = Trim(Right(strWholeAddress, (Len(strWholeAddress) -
intFS)))

'capture the City
strCity = Left(strWholeAddress, Len(strWholeAddress) - 9)

'capture the state and zip
strZip = Right(strWholeAddress, 5)
strState = Mid(strWholeAddress, Len(strWholeAddress) - 7, 2)

'capture email
strEmail = Range("J" & i)

'check for duplicate entries in database

'create a sql string that pulls identical records
strSQL = "SELECT * " & _
"FROM qryDonorAddresses " & _
"WHERE LastName Like '" & Left(strLastName, 3) & "*' " & _
"AND Address1 Like '" & Left(strAddress, 5) & "*'"

'set the recordset to such
Set rs = db.OpenRecordset(strSQL)
Set rsDonors = db.OpenRecordset("tblDonors")
Set rsAddresses = db.OpenRecordset("tblAddresses")

'loop through the records (if there are any) and inform the user
Do Until rs.EOF

'if the address is obviously the same (10 characters) then don't inform
the
user
If Left(strAddress, 10) = Left(rs.Fields("Address1"), 10) Then
blnFound = True
rsID = rs![tblDonors.DonorID]
Exit Do
End If

If MsgBox("This record exists:" & vbCrLf & _
rs.Fields("FirstName") & " " & rs.Fields("LastName") & vbCrLf & _
rs.Fields("Address1") & vbCrLf & vbCrLf & _
"Your Record is:" & vbCrLf & _
strFirstName & " " & strLastName & vbCrLf & _
strAddress & vbCrLf & vbCrLf & _
"Is this the same person?", vbYesNo + vbQuestion) = vbYes Then

'if a record IS found then update the email ONLY
'if blnFound is true then the AddNew will not occur
blnFound = True
rs.Edit
rs!Emailaddress = strEmail
rs.Update
Exit Do
End If
rs.MoveNext
Loop
Debug.Print i
Debug.Print strFirstName
Debug.Print strMiddleName
Debug.Print strLastName
Debug.Print strAddress
Debug.Print strCity
Debug.Print strState
Debug.Print strZip
Debug.Print strEmail




'if no record was found then add the record to the db
If blnFound = False Then
With rsDonors
.AddNew
rsDonors!FirstName = strFirstName
rsDonors!LastName = strLastName
rsDonors!MiddleName = strMiddleName
rsDonors!Emailaddress = strEmail
.Update
End With

rsDonors.Bookmark = rsDonors.LastModified
rsID = rsDonors!DonorID

With rsAddresses
.AddNew
rsAddresses!DonorID = rsID
rsAddresses!Address1 = strAddress
rsAddresses!City = strCity
rsAddresses!State = strState
rsAddresses!Zip = strZip
.Update
End With
End If

'add the contribution record to the db

strDescription = strDescription & Range("D" & i)
strDescription = Left(strDescription, Len(strDescription) - 7)

lngKey = rsID
strSQL = "SELECT * FROM tblContributions"
Set rsContributions = db.OpenRecordset(strSQL)
With rsContributions
.AddNew
rsContributions!DonorID = lngKey
rsContributions!Date = dtContributionDate
rsContributions!Description = strDescription
rsContributions!Amount = Range("L" & i)
rsContributions!GLCode = lngGLCode
rsContributions!Batch = strBatch
rsContributions!CheckNumber = strCheck
rsContributions!EventYear = Right(strContributionDate, 4)
.Update
End With

rsContributions.Bookmark = rsContributions.LastModified

rsID = rsContributions!ContributionID

Select Case lngGLCode
Case 55100, 55200, 55300, 55400, 55500
strCode = "WLK-SPONS"
Case 51200
strCode = "TRANSPLANT"
End Select

Set rsIdentities = db.OpenRecordset("tblContributionIdentities")
With rsIdentities
.AddNew
rsIdentities!ContributionID = rsID
rsIdentities!ContributionCode = strCode
.Update
End With


'reset description string
intFS = InStr(1, strDescription, "-")
intFS = Len(strDescription) - intFS

strDescription = Left(strDescription, Len(strDescription) - intFS + 1)

'zero out capture strings
strName = ""
strWholeAddress = ""

'blnFound to false RESET
blnFound = False

'increment to next row
i = i + 1
Loop

'zero out db variables
Set db = Nothing
Set rs = Nothing
Set rsDonors = Nothing
Set rsAddresses = Nothing
Set rsContributions = Nothing
Set rsIdentities = Nothing
End Sub

Private Function CheckBatch(strCD As String) As String
Dim db As DAO.Database
Dim rs As DAO.Recordset
'Used for checking on open batch numbers
'date for filtering SQL
Dim dtCheckDate As Date

'making date a string for SQL
dtCheckDate = FormatDateTime(strCD, vbShortDate)

'SQL for recordset
Dim strSQL As String

strSQL = "SELECT tblContributions.Batch, " & _
"tblContributions.Date " & _
"FROM tblContributions GROUP BY tblContributions.Batch, " & _
"tblContributions.Date " & _
"HAVING tblContributions.Date = " & Chr(35) & _
dtCheckDate & Chr(35) & " ORDER BY tblContributions.Batch;"

'record set variable
Set db = OpenDatabase("C:\Contributions_FrontEnd.mdb")
Set rs = db.OpenRecordset(strSQL)

'create a batch date style date MMDDYY
Dim strBDMonth As String
Dim strBDDay As String
Dim strBDYear As String
strBDYear = Right(strCD, 2)
Dim intFM As Integer
Dim intSM As Integer
intFM = InStr(1, strCD, "/")
intSM = InStr(intFM + 1, strCD, "/")
If intFM = 2 Then
strBDMonth = "0" & Left(strCD, 1)
Else
strBDMonth = Left(strCD, 2)
End If
If (intFM - intSM) = 2 Then
strBDDay = "0" & Mid(strCD, intFM + 1, 1)
Else
strBDDay = Mid(strCD, intFM + 1, 2)
End If

'check for records
'if none than let the user know and kick out of code
If rs.EOF Then
CheckBatch = "FG" & strBDMonth & _
strBDDay & strBDYear & "01"
Set db = Nothing
Set rs = Nothing
Exit Function
End If

'if there are records, loop through and build the correct/highest sequence
Dim strSequence As String
Do While Not rs.EOF
If Val(Right(rs.Fields(0), 1)) > Val(strSequence) Then
strSequence = Val(Right(rs.Fields(0), 1))
rs.MoveNext
Else
rs.MoveNext
End If
Loop
CheckBatch = "FG" & strBDMonth & _
strBDDay & strBDYear & "0" & Val(strSequence) + 1
Set db = Nothing
Set rs = Nothing

End Function
 
C

C Hayes

Jeanette

I'm having trouble with a function not identified.

'FindColumnTitle'

am I missing a 'reference' or is this a custom function?
 
J

Jeanette Cunningham

Hi,
the code shown below was included in my original reply to your question as
an example of how to do it.
'now format the report

'get the count of how many columns for this report
lngColumnCount = Nz(DMax("[ColumnNo]", "tlkpXLRptProps", "[ReportID] = "
& lngReportID), 0)
lngAlignR = Nz(DLookup("[AlignR]", "tlkpXLReport", "[ReportID] = " &
lngReportID), 0)
'Debug.Print lngAlignR


'if any columns with costs then right align them
If lngAlignR > 0 Then
With objXLws.Cells
.columns(lngAlignR).horizontalalignment = xlRight
'centre the heading for the column with costs
.Range(.Cells(1, lngAlignR), .Cells(1,
lngAlignR)).horizontalalignment = xlCentre
End With
End If


With objXLws.Cells
'do column headings
For intI = 1 To lngColumnCount
.Range(.Cells(1, intI), .Cells(1, intI)) = _
FindColumnTitle(lngColumnNo:=intI, lngReportNo:=lngReportID)
Next intI

the code uses custom functions I wrote to suit the tables in my particular
application, and would not work in your app.

I had a couple of lookup tables I used for formatting the worksheet.
I used the code below to run about 100 different reports and stored their
column titles, how many columns and column width in a table in Access.
The code is an example of how to do it, it is not meant to be copied exactly
and used.

here are the functions which looked up info in the lookup tables:

'--------------------------------------------------------
' Inputs: column no., template no
' Output: column width as single
' Purpose: if the column title is missing from tlkpXLRptProps
' use ""
' Created By: JEC 14/11/2007
' DateModified: 14/11/2007
'--------------------------------------------------------

Private Function FindColumnTitle(ByVal lngColumnNo As Long, lngReportNo As
Long) As String
On Error GoTo FunctionErr
pstrProc = "FindColumnTitle"
pstrSubProc = "FindColumnTitle"
Dim strCriteria As String

strCriteria = "(tlkpXLRptProps.ColumnNo = " & lngColumnNo & ") " _
& "And (tlkpXLRptProps.ReportID = " & lngReportNo & ")"
'Debug.Print strCriteria
FindColumnTitle = Nz(DLookup("[ColumnTitle]", "tlkpXLRptProps",
strCriteria), "")
'Debug.Print FindColumnTitle

FunctionExit:
Exit Function
FunctionErr:
Call fnFormErrHandler(pstrProc, pstrSubProc, pstrMdl, Err)
Resume FunctionExit
End Function


'--------------------------------------------------------
' Inputs: column no., template no
' Output: column width as single
' Purpose: if the column width is missing from tlkpXLRptProps
' use default of 8.43
' Created By: JEC 14/11/2007
' DateModified: 14/11/2007
'--------------------------------------------------------

Private Function FindColumnWidth(ByVal lngColumnNo As Long, lngTemplateNo As
Long) As Single
On Error GoTo FunctionErr
pstrProc = "FindColumnWidth"
pstrSubProc = "FindColumnWidth"
Dim strCriteria As String

strCriteria = "(tlkpXLRptProps.ColumnNo = " & lngColumnNo & ") " _
& "And (tlkpXLRptProps.ReportID = " & lngTemplateNo & ")"
'debug.print strCriteria
FindColumnWidth = Nz(DLookup("[ColumnWidthSng]", "tlkpXLRptProps",
strCriteria), 8.43)
'debug.print FindColumnWidth

FunctionExit:
Exit Function
FunctionErr:
Call fnFormErrHandler(pstrProc, pstrSubProc, pstrMdl, Err)
Resume FunctionExit
End Function


'--------------------------------------------------------
' Inputs:
' Output:
' Purpose: given a column no, look up the corresponding
' alphabet letter in tlkpXLRptRange
' Created By: JEC 14/11/2007
' DateModified: 14/11/2007
'--------------------------------------------------------

Public Function RangeLetter(lngColumnNo) As String
On Error GoTo FunctionErr
pstrProc = "RangeColumn"
pstrSubProc = "RangeColumn"
Dim strCriteria As String

strCriteria = "[ColumnNo] = " & lngColumnNo
RangeLetter = Nz(DLookup("[AlphaLetter]", "tlkpXLRptRange",
strCriteria), "A")

FunctionExit:
Exit Function

FunctionErr:
Call fnFormErrHandler(pstrProc, pstrSubProc, pstrMdl, Err)
Resume FunctionExit
End Function

Jeanette Cunningham
 
C

C Hayes

I'm sorry, I misunderstood.

The process of reading, analyzing a spreadsheet is complex, thus my original
solution was lengthy.

The file dialog portion I have got working. With the help of others on this
site I even accomplised getting a dialog form for selecting the sheet name
from an Excel workbook (which was sparked by your example.)

Thanks

Jeanette Cunningham said:
Hi,
the code shown below was included in my original reply to your question as
an example of how to do it.
'now format the report

'get the count of how many columns for this report
lngColumnCount = Nz(DMax("[ColumnNo]", "tlkpXLRptProps", "[ReportID] = "
& lngReportID), 0)
lngAlignR = Nz(DLookup("[AlignR]", "tlkpXLReport", "[ReportID] = " &
lngReportID), 0)
'Debug.Print lngAlignR


'if any columns with costs then right align them
If lngAlignR > 0 Then
With objXLws.Cells
.columns(lngAlignR).horizontalalignment = xlRight
'centre the heading for the column with costs
.Range(.Cells(1, lngAlignR), .Cells(1,
lngAlignR)).horizontalalignment = xlCentre
End With
End If


With objXLws.Cells
'do column headings
For intI = 1 To lngColumnCount
.Range(.Cells(1, intI), .Cells(1, intI)) = _
FindColumnTitle(lngColumnNo:=intI, lngReportNo:=lngReportID)
Next intI

the code uses custom functions I wrote to suit the tables in my particular
application, and would not work in your app.

I had a couple of lookup tables I used for formatting the worksheet.
I used the code below to run about 100 different reports and stored their
column titles, how many columns and column width in a table in Access.
The code is an example of how to do it, it is not meant to be copied exactly
and used.

here are the functions which looked up info in the lookup tables:

'--------------------------------------------------------
' Inputs: column no., template no
' Output: column width as single
' Purpose: if the column title is missing from tlkpXLRptProps
' use ""
' Created By: JEC 14/11/2007
' DateModified: 14/11/2007
'--------------------------------------------------------

Private Function FindColumnTitle(ByVal lngColumnNo As Long, lngReportNo As
Long) As String
On Error GoTo FunctionErr
pstrProc = "FindColumnTitle"
pstrSubProc = "FindColumnTitle"
Dim strCriteria As String

strCriteria = "(tlkpXLRptProps.ColumnNo = " & lngColumnNo & ") " _
& "And (tlkpXLRptProps.ReportID = " & lngReportNo & ")"
'Debug.Print strCriteria
FindColumnTitle = Nz(DLookup("[ColumnTitle]", "tlkpXLRptProps",
strCriteria), "")
'Debug.Print FindColumnTitle

FunctionExit:
Exit Function
FunctionErr:
Call fnFormErrHandler(pstrProc, pstrSubProc, pstrMdl, Err)
Resume FunctionExit
End Function


'--------------------------------------------------------
' Inputs: column no., template no
' Output: column width as single
' Purpose: if the column width is missing from tlkpXLRptProps
' use default of 8.43
' Created By: JEC 14/11/2007
' DateModified: 14/11/2007
'--------------------------------------------------------

Private Function FindColumnWidth(ByVal lngColumnNo As Long, lngTemplateNo As
Long) As Single
On Error GoTo FunctionErr
pstrProc = "FindColumnWidth"
pstrSubProc = "FindColumnWidth"
Dim strCriteria As String

strCriteria = "(tlkpXLRptProps.ColumnNo = " & lngColumnNo & ") " _
& "And (tlkpXLRptProps.ReportID = " & lngTemplateNo & ")"
'debug.print strCriteria
FindColumnWidth = Nz(DLookup("[ColumnWidthSng]", "tlkpXLRptProps",
strCriteria), 8.43)
'debug.print FindColumnWidth

FunctionExit:
Exit Function
FunctionErr:
Call fnFormErrHandler(pstrProc, pstrSubProc, pstrMdl, Err)
Resume FunctionExit
End Function


'--------------------------------------------------------
' Inputs:
' Output:
' Purpose: given a column no, look up the corresponding
' alphabet letter in tlkpXLRptRange
' Created By: JEC 14/11/2007
' DateModified: 14/11/2007
'--------------------------------------------------------

Public Function RangeLetter(lngColumnNo) As String
On Error GoTo FunctionErr
pstrProc = "RangeColumn"
pstrSubProc = "RangeColumn"
Dim strCriteria As String

strCriteria = "[ColumnNo] = " & lngColumnNo
RangeLetter = Nz(DLookup("[AlphaLetter]", "tlkpXLRptRange",
strCriteria), "A")

FunctionExit:
Exit Function

FunctionErr:
Call fnFormErrHandler(pstrProc, pstrSubProc, pstrMdl, Err)
Resume FunctionExit
End Function

Jeanette Cunningham

C Hayes said:
Jeanette

I'm having trouble with a function not identified.

'FindColumnTitle'

am I missing a 'reference' or is this a custom function?
 
J

Jeanette Cunningham

Yes, their is a lot of coding involved for you, and getting it to work can
be a slow process as there are many different steps involved.

Jeanette Cunningham

C Hayes said:
I'm sorry, I misunderstood.

The process of reading, analyzing a spreadsheet is complex, thus my
original
solution was lengthy.

The file dialog portion I have got working. With the help of others on
this
site I even accomplised getting a dialog form for selecting the sheet name
from an Excel workbook (which was sparked by your example.)

Thanks

Jeanette Cunningham said:
Hi,
the code shown below was included in my original reply to your question
as
an example of how to do it.
'now format the report

'get the count of how many columns for this report
lngColumnCount = Nz(DMax("[ColumnNo]", "tlkpXLRptProps", "[ReportID]
= "
& lngReportID), 0)
lngAlignR = Nz(DLookup("[AlignR]", "tlkpXLReport", "[ReportID] = " &
lngReportID), 0)
'Debug.Print lngAlignR


'if any columns with costs then right align them
If lngAlignR > 0 Then
With objXLws.Cells
.columns(lngAlignR).horizontalalignment = xlRight
'centre the heading for the column with costs
.Range(.Cells(1, lngAlignR), .Cells(1,
lngAlignR)).horizontalalignment = xlCentre
End With
End If


With objXLws.Cells
'do column headings
For intI = 1 To lngColumnCount
.Range(.Cells(1, intI), .Cells(1, intI)) = _
FindColumnTitle(lngColumnNo:=intI,
lngReportNo:=lngReportID)
Next intI

the code uses custom functions I wrote to suit the tables in my
particular
application, and would not work in your app.

I had a couple of lookup tables I used for formatting the worksheet.
I used the code below to run about 100 different reports and stored their
column titles, how many columns and column width in a table in Access.
The code is an example of how to do it, it is not meant to be copied
exactly
and used.

here are the functions which looked up info in the lookup tables:

'--------------------------------------------------------
' Inputs: column no., template no
' Output: column width as single
' Purpose: if the column title is missing from tlkpXLRptProps
' use ""
' Created By: JEC 14/11/2007
' DateModified: 14/11/2007
'--------------------------------------------------------

Private Function FindColumnTitle(ByVal lngColumnNo As Long, lngReportNo
As
Long) As String
On Error GoTo FunctionErr
pstrProc = "FindColumnTitle"
pstrSubProc = "FindColumnTitle"
Dim strCriteria As String

strCriteria = "(tlkpXLRptProps.ColumnNo = " & lngColumnNo & ") " _
& "And (tlkpXLRptProps.ReportID = " & lngReportNo & ")"
'Debug.Print strCriteria
FindColumnTitle = Nz(DLookup("[ColumnTitle]", "tlkpXLRptProps",
strCriteria), "")
'Debug.Print FindColumnTitle

FunctionExit:
Exit Function
FunctionErr:
Call fnFormErrHandler(pstrProc, pstrSubProc, pstrMdl, Err)
Resume FunctionExit
End Function


'--------------------------------------------------------
' Inputs: column no., template no
' Output: column width as single
' Purpose: if the column width is missing from tlkpXLRptProps
' use default of 8.43
' Created By: JEC 14/11/2007
' DateModified: 14/11/2007
'--------------------------------------------------------

Private Function FindColumnWidth(ByVal lngColumnNo As Long, lngTemplateNo
As
Long) As Single
On Error GoTo FunctionErr
pstrProc = "FindColumnWidth"
pstrSubProc = "FindColumnWidth"
Dim strCriteria As String

strCriteria = "(tlkpXLRptProps.ColumnNo = " & lngColumnNo & ") " _
& "And (tlkpXLRptProps.ReportID = " & lngTemplateNo & ")"
'debug.print strCriteria
FindColumnWidth = Nz(DLookup("[ColumnWidthSng]", "tlkpXLRptProps",
strCriteria), 8.43)
'debug.print FindColumnWidth

FunctionExit:
Exit Function
FunctionErr:
Call fnFormErrHandler(pstrProc, pstrSubProc, pstrMdl, Err)
Resume FunctionExit
End Function


'--------------------------------------------------------
' Inputs:
' Output:
' Purpose: given a column no, look up the corresponding
' alphabet letter in tlkpXLRptRange
' Created By: JEC 14/11/2007
' DateModified: 14/11/2007
'--------------------------------------------------------

Public Function RangeLetter(lngColumnNo) As String
On Error GoTo FunctionErr
pstrProc = "RangeColumn"
pstrSubProc = "RangeColumn"
Dim strCriteria As String

strCriteria = "[ColumnNo] = " & lngColumnNo
RangeLetter = Nz(DLookup("[AlphaLetter]", "tlkpXLRptRange",
strCriteria), "A")

FunctionExit:
Exit Function

FunctionErr:
Call fnFormErrHandler(pstrProc, pstrSubProc, pstrMdl, Err)
Resume FunctionExit
End Function

Jeanette Cunningham

C Hayes said:
Jeanette

I'm having trouble with a function not identified.

'FindColumnTitle'

am I missing a 'reference' or is this a custom function?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top