E
emquick
I've been scratching my head trying to figure out why the runtime error
occurs when the DAvg (or DMax or DMin) function call is made in CalcAvg (or
CalcMax or CalcMin) function. I try to avoid creating and deleting (query
and recordset) objects since they make the db really bloated while the module
is running.
I appreciate any help. Below is the code.
Thanks.
Elie Quick
Function GetHML_noqry()
Dim db As Database
Dim rs As Recordset, rs1 As Recordset
Dim str As String, ans As String
'import data
'prompt for file name, if file name exists, then existing data in table wil
be wiped to allow for new data
str = InputBox("Enter filename (full path) to be imported")
If IsNull(str) Or str = "" Then
MsgBox ("No filename, exit function")
Exit Function
Else
DoCmd.DeleteObject acTable, "currlist"
DoCmd.CopyObject , "currlist", acTable, "RE_tmplt"
DoCmd.TransferText acImportDelim, "RE_Import_Spec", "currlist", str,
-1
End If
ans = MsgBox("Do you need to import base data?", vbYesNo)
If ans = vbYes Then
DoCmd.DeleteObject acTable, "currbd"
DoCmd.CopyObject , "currbd", acTable, "BD_tmplt"
str = InputBox("Enter filename (full path) to be imported")
DoCmd.TransferText , "REbd_Import_Spec", "currbd", str, -1
End If
'setup query and prepare table for calucation and update
DoCmd.Hourglass True
Set db = CurrentDb
Set rs = db.OpenRecordset("currlist")
rs.MoveFirst
Do While Not rs.EOF
rs.Edit
rs!Average = CalcAvg(rs!ar, rs!rms, rs!br, rs!bth)
rs!High = CalcMax(rs!ar, rs!rms, rs!br, rs!bth)
rs!Low = CalcMin(rs!ar, rs!rms, rs!br, rs!bth)
rs.Update
rs.MoveNext
Loop
DoCmd.Hourglass False
'export data to Excel
str = ""
Do While (IsNull(str) Or str = "")
str = InputBox("Exporting to Excel file format. Please enter
filename (full path)")
If IsNull(str) Or str = "" Then
ans = MsgBox("Filename is required", vbOKOnly)
End If
Loop
DoCmd.TransferSpreadsheet acExport, , "currlist", str, -1
'close db objects and reset table
rs.Close
db.Close
End Function
Function CalcAvg(ByVal area As Double, ByVal rooms As Double, ByVal bed As
Double, ByVal bath As Double) As Double
Dim str As String
str = "[AR]=" & area & " AND [RMS]=" & rooms & " AND [BD]=" & bed & "
AND [BTH]=" & bath
CalcAvg = DAvg("[LP]", "currbd", str)
End Function
Function CalcMax(ByVal area As Double, ByVal rooms As Double, ByVal bed As
Double, ByVal bath As Double) As Double
Dim str As String
str = "[AR]=" & area & " AND [RMS]=" & rooms & " AND [BD]=" & bed & "
AND [BTH]=" & bath
CalcMax = DMax("[LP]", "currbd", str)
End Function
Function CalcMin(ByVal area As Double, ByVal rooms As Double, ByVal bed As
Double, ByVal bath As Double) As Double
Dim str As String
str = "[AR]=" & area & " AND [RMS]=" & rooms & " AND [BD]=" & bed & "
AND [BTH]=" & bath
CalcMin = DMin("[LP]", "currbd", str)
End Function
occurs when the DAvg (or DMax or DMin) function call is made in CalcAvg (or
CalcMax or CalcMin) function. I try to avoid creating and deleting (query
and recordset) objects since they make the db really bloated while the module
is running.
I appreciate any help. Below is the code.
Thanks.
Elie Quick
Function GetHML_noqry()
Dim db As Database
Dim rs As Recordset, rs1 As Recordset
Dim str As String, ans As String
'import data
'prompt for file name, if file name exists, then existing data in table wil
be wiped to allow for new data
str = InputBox("Enter filename (full path) to be imported")
If IsNull(str) Or str = "" Then
MsgBox ("No filename, exit function")
Exit Function
Else
DoCmd.DeleteObject acTable, "currlist"
DoCmd.CopyObject , "currlist", acTable, "RE_tmplt"
DoCmd.TransferText acImportDelim, "RE_Import_Spec", "currlist", str,
-1
End If
ans = MsgBox("Do you need to import base data?", vbYesNo)
If ans = vbYes Then
DoCmd.DeleteObject acTable, "currbd"
DoCmd.CopyObject , "currbd", acTable, "BD_tmplt"
str = InputBox("Enter filename (full path) to be imported")
DoCmd.TransferText , "REbd_Import_Spec", "currbd", str, -1
End If
'setup query and prepare table for calucation and update
DoCmd.Hourglass True
Set db = CurrentDb
Set rs = db.OpenRecordset("currlist")
rs.MoveFirst
Do While Not rs.EOF
rs.Edit
rs!Average = CalcAvg(rs!ar, rs!rms, rs!br, rs!bth)
rs!High = CalcMax(rs!ar, rs!rms, rs!br, rs!bth)
rs!Low = CalcMin(rs!ar, rs!rms, rs!br, rs!bth)
rs.Update
rs.MoveNext
Loop
DoCmd.Hourglass False
'export data to Excel
str = ""
Do While (IsNull(str) Or str = "")
str = InputBox("Exporting to Excel file format. Please enter
filename (full path)")
If IsNull(str) Or str = "" Then
ans = MsgBox("Filename is required", vbOKOnly)
End If
Loop
DoCmd.TransferSpreadsheet acExport, , "currlist", str, -1
'close db objects and reset table
rs.Close
db.Close
End Function
Function CalcAvg(ByVal area As Double, ByVal rooms As Double, ByVal bed As
Double, ByVal bath As Double) As Double
Dim str As String
str = "[AR]=" & area & " AND [RMS]=" & rooms & " AND [BD]=" & bed & "
AND [BTH]=" & bath
CalcAvg = DAvg("[LP]", "currbd", str)
End Function
Function CalcMax(ByVal area As Double, ByVal rooms As Double, ByVal bed As
Double, ByVal bath As Double) As Double
Dim str As String
str = "[AR]=" & area & " AND [RMS]=" & rooms & " AND [BD]=" & bed & "
AND [BTH]=" & bath
CalcMax = DMax("[LP]", "currbd", str)
End Function
Function CalcMin(ByVal area As Double, ByVal rooms As Double, ByVal bed As
Double, ByVal bath As Double) As Double
Dim str As String
str = "[AR]=" & area & " AND [RMS]=" & rooms & " AND [BD]=" & bed & "
AND [BTH]=" & bath
CalcMin = DMin("[LP]", "currbd", str)
End Function