That's pretty much the problem rpeare. I'm building for a multi user environment - so I need to reliably get the key. The way I'm doing it now works, it's just slow and I was hoping to speed it up.
June7 - to explain what I'm trying to do:
I have 3 inter-related tables. We'll call them Process 1, Process 2, and Incident. Incidents are the central point - any time a Process is created, an Incident needs to be created. 1-to-1-to-1. Any Process MUST have an Incident, but it doesn't necessarily require the other process to be initiated. An Incident can have one of each Process active.
So Form for Process 1 is loaded, it runs this code (it has been edited so only the really relevant parts are present):
Code:
Private Sub Form_Load()
Dim key As String
key = IIf(IsNull(Me.OpenArgs), 341, Me.OpenArgs)
If LoadIMSection(key) Then
End Sub
Private Function LoadIMSection(key As String) As Boolean
LoadIMSection = False
If key = "New" Then
SetupNewProcess
LoadIMSection = True
Exit Function
End If
(lots more code here, I'll post below when it's relevent)
End Function
Private Sub SetupNewProcess()
SetDefaultNew
CreateNewRecord
End Sub
Private Sub CreateNewRecord()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim def As DAO.QueryDef
Set db = CurrentDb
Set rs = db.OpenRecordset("qryIMProcess_NEW")
rs.AddNew
Me.txtIMID.value = rs!lngIMIDCnt
cboIM = GetName
cboCA = GetName
cboScribe = GetName
rs!strIM = GetName
rs!strCA = GetName
rs!strScribe = GetName
rs.update
rs.Close
End Sub
This creates the initial record that most of the form handles. At this point, not far different from having it bound to a query. However, I can't bind the form because of record locking - I need multiple people to access the same record through the same form.
So then whenever I save the form, a lot of data is aved to the Incident table instead of the Process table. Here's how that's handled:
Code:
Private Function NoAssociatedIncident() As Boolean
If Me.cboIncidentID.value = "" Or IsNull(cboIncidentID.value) Then
Debug.Print "no associated incident"
NoAssociatedIncident = True
End If
End Function
Private Function SaveIMSection(key As String) As Boolean
On Error GoTo ErrorHandler
If NoAssociatedIncident Then
CreateIncident
End If
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim def As DAO.QueryDef
Set db = CurrentDb
Set def = db.QueryDefs![qryIMProcess_PARAM]
def.Parameters![IMID] = key
Set rs = def.OpenRecordset
If rs.RecordCount = 0 Then
Exit Function
End If
rs.Edit
rs!strIM = cboIM.value
'TO SAVE EYE BLEED I'M CUTTING OUT THE ASSIGNMENTS
'Theres a bunch like this, like 30 items that get assigned rs!field = control.value
rs.update
rs.Close
SetIncidentManager
ErrorHandlerExit:
Exit Function
ErrorHandler:
If Err = 3021 Then
'No Current Recordset
MsgBox "There is no current record. Either the record has been deleted or an error occurred. Please check with the Incident Manager for further details."
rs.Close
DoCmd.Close "frmIncidentManagement"
Exit Function
End If
End Function
Private Sub CreateIncident()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("qryNewIncident")
rs.AddNew
rs!lngIMIDCnt = Me.txtIMID
Me.cboIncidentID.value = rs!lngIncidentID
Me.cboIncidentID.Locked = True
rs.update
rs.Close
Dim def As DAO.QueryDef
Set def = db.QueryDefs![qryIMOnly_tblIMProcess_PARAM]
def.Parameters![IMID] = txtIMID
Set rs = def.OpenRecordset
rs.Edit
rs!lngIncidentID = cboIncidentID.value
rs.update
rs.Close
End Sub
So if it attempts to save to the Incident Table but finds no associated record, it creates one this way.