C
Chris
Hi there,
I posted a message here a few days back regarding
subforms with no records causing an error. The reply I got
seemed to solve the problem I had. Now, the problem is
back but in a different form. I think that I'm doing
something fundementally wrong and perhaps I have too many
controls on my form. The problem is as follows:
I have a control in a subform that calls a function to
calculate the current status of the record. The status of
each record is stored in a separate table called
tblHistory. The recordsource for my subform runs on a
query based on a table called tblPlanningApplications. The
table tblPlanningApplications has a field called
currentstatus which stores the ID of the appropriate
record in the tblHistory table. The control calls a
function to find out the current status and deals with it
if it is null (or if it is an error: this happens only
when the record count is 0). This in turn calls a function
that loops through the history table and returns the
current status. The problem is that it calculates
correctly sometimes and at apparently random intervals it
calculates as #Name?. When I loop through the code it
always comes out correctly even though it displays #Name?
as the result.
Could this be as a result of too many controls on the form?
Am I calling too much code? Could anyone tell me why the
control is behaving this way? Given the exact same
parameters everytime the result varies. Any help in
solving this problem would be appreciated greatly. I have
posted the code below in case it helps. Thanks in advance
for any help/advice/comments.
Regards,
Chris Kelly.
The control causing the problem is bound to the following
expression.
=GetStateName(GetHistoryState([Forms]![frmProjects]!
[sfSubJobs].[Form]![Combo88].[Value]))
CODE
Function getStateName(num) As String
Dim db As Database
Dim rst As Recordset
Dim qdf As QueryDef
Dim str As String
On Error GoTo getStateName_Error
If IsNull(num) Then
num = 16 ' The code for the unknown state: UNK
End If
' Select records from the PlanningState table. This
gives a 3 letter description of the state based on an index
str = "SELECT PlanningStateID, PlanningState FROM
tblPlanningStates WHERE (PlanningStateID = " & num & ");"
Set db = CurrentDb
Set qdf = db.CreateQueryDef("", str)
Set rst = qdf.OpenRecordset
If rst.RecordCount <> 0 Then ' If there are no records
return UNKnown
With rst
.MoveFirst
getStateName = !PlanningState
End With
Else
getStateName = "UNK"
End If
On Error GoTo 0
Exit Function
getStateName_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description
& ") in procedure getStateName of Module modGlobals"
End Function
Function GetHistoryState(currstat) As Long
Dim sqlstr As String
Dim db As Database
Dim rst As Recordset
Dim qdf As QueryDef
Dim bStateSet As Boolean
On Error GoTo GetHistoryState_Error
' Select all the records with the same planningAppID
as the selected record on the subform
sqlstr = "SELECT PlanningHistoryID, PlanningAppID,
State, DateEntered, EmpID From tblPlanningHistory WHERE
(PlanningAppID=" & Forms![frmProjects]![sfSubJobs].Form!
[PlanningAppID] & ");"
Set db = CurrentDb
Set qdf = db.CreateQueryDef("", sqlstr)
Set rst = qdf.OpenRecordset
If IsError(currstat) Then
GetHistoryState = 16 ' On error return code for
unknown
Else
If rst.RecordCount <> 0 Then
With rst
.MoveFirst
Do Until .EOF
If currstat = !PlanningHistoryID Then
GetHistoryState = !State
bStateSet = True
End If
.MoveNext
Loop
If bStateSet = False Then
GetHistoryState = 16
End If
End With
Else
GetHistoryState = 16
End If
End If
On Error GoTo 0
Exit Function
GetHistoryState_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description
& ") in procedure GetHistoryState of Module modGlobals"
End Function
I posted a message here a few days back regarding
subforms with no records causing an error. The reply I got
seemed to solve the problem I had. Now, the problem is
back but in a different form. I think that I'm doing
something fundementally wrong and perhaps I have too many
controls on my form. The problem is as follows:
I have a control in a subform that calls a function to
calculate the current status of the record. The status of
each record is stored in a separate table called
tblHistory. The recordsource for my subform runs on a
query based on a table called tblPlanningApplications. The
table tblPlanningApplications has a field called
currentstatus which stores the ID of the appropriate
record in the tblHistory table. The control calls a
function to find out the current status and deals with it
if it is null (or if it is an error: this happens only
when the record count is 0). This in turn calls a function
that loops through the history table and returns the
current status. The problem is that it calculates
correctly sometimes and at apparently random intervals it
calculates as #Name?. When I loop through the code it
always comes out correctly even though it displays #Name?
as the result.
Could this be as a result of too many controls on the form?
Am I calling too much code? Could anyone tell me why the
control is behaving this way? Given the exact same
parameters everytime the result varies. Any help in
solving this problem would be appreciated greatly. I have
posted the code below in case it helps. Thanks in advance
for any help/advice/comments.
Regards,
Chris Kelly.
The control causing the problem is bound to the following
expression.
=GetStateName(GetHistoryState([Forms]![frmProjects]!
[sfSubJobs].[Form]![Combo88].[Value]))
CODE
Function getStateName(num) As String
Dim db As Database
Dim rst As Recordset
Dim qdf As QueryDef
Dim str As String
On Error GoTo getStateName_Error
If IsNull(num) Then
num = 16 ' The code for the unknown state: UNK
End If
' Select records from the PlanningState table. This
gives a 3 letter description of the state based on an index
str = "SELECT PlanningStateID, PlanningState FROM
tblPlanningStates WHERE (PlanningStateID = " & num & ");"
Set db = CurrentDb
Set qdf = db.CreateQueryDef("", str)
Set rst = qdf.OpenRecordset
If rst.RecordCount <> 0 Then ' If there are no records
return UNKnown
With rst
.MoveFirst
getStateName = !PlanningState
End With
Else
getStateName = "UNK"
End If
On Error GoTo 0
Exit Function
getStateName_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description
& ") in procedure getStateName of Module modGlobals"
End Function
Function GetHistoryState(currstat) As Long
Dim sqlstr As String
Dim db As Database
Dim rst As Recordset
Dim qdf As QueryDef
Dim bStateSet As Boolean
On Error GoTo GetHistoryState_Error
' Select all the records with the same planningAppID
as the selected record on the subform
sqlstr = "SELECT PlanningHistoryID, PlanningAppID,
State, DateEntered, EmpID From tblPlanningHistory WHERE
(PlanningAppID=" & Forms![frmProjects]![sfSubJobs].Form!
[PlanningAppID] & ");"
Set db = CurrentDb
Set qdf = db.CreateQueryDef("", sqlstr)
Set rst = qdf.OpenRecordset
If IsError(currstat) Then
GetHistoryState = 16 ' On error return code for
unknown
Else
If rst.RecordCount <> 0 Then
With rst
.MoveFirst
Do Until .EOF
If currstat = !PlanningHistoryID Then
GetHistoryState = !State
bStateSet = True
End If
.MoveNext
Loop
If bStateSet = False Then
GetHistoryState = 16
End If
End With
Else
GetHistoryState = 16
End If
End If
On Error GoTo 0
Exit Function
GetHistoryState_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description
& ") in procedure GetHistoryState of Module modGlobals"
End Function