In my database there is a form that displays records filtered by an abbreviation you can choose from in a combo box. Now in some cases I will want to open another form with the same records as displayed before, but this time there need to be two extra columns and the records (specifically the two new columns) have to be editable. So far my approach looks like this: I fetch the data from the previous form, put it in a new Recordset and add the two columns. Looks as follows:
Dim cpRS As New ADODB.Recordset, RS As DAO.Recordset, cb As checkBox, addr As String
'Creating copy of previously displayed result table
Set cpRS = New ADODB.Recordset
With cpRS
'all the fields needed. I shortened this because it's too long and has no important information
.Fields.Append "some_value", adInteger
.Fields.Append "some_value", adInteger
.Fields.Append "some_value", adSmallInt, , adFldIsNullable
'new Fields for temporary purposes
.Fields.Append "first_new_field_checkbox", adBoolean
.Fields.Append "second_new_field_textbox", adVarChar, 50
.CursorLocation = adUseClient
.Open , , adOpenKeyset, adLockPessimistic, 8
End With
'get result set of previous window by applying filter to the same query used before
Dim argv() As String
Dim argRest As String
Dim qdef As DAO.QueryDef
Dim restrictedQuery As String
'When opening this form I hand over OpenArgs which i restore here
'took the code out but "argv" and "argRest" will be used later
'this is the query that is used in the previous form. i need an extra where clause though so i had to rewrite it.
restrictedQuery = "some very long SQL statement I feel I don't need to put here because it doesn't contribute to the problem." & _
"If I'm incorrect, please let me know and I will rewrite it to protect the data in it"
Set qdef = CurrentDb.CreateQueryDef("")
qdef.SQL = restrictedQuery
Set RS = qdef.OpenRecordset
Set RS = CurrentDb.OpenRecordset(restrictedQuery, dbOpenSnapshot)
RS.MoveLast
RS.MoveFirst
If RS.RecordCount = 0 Then
MsgBox "some error text", vbOKOnly, "error title"
DoCmd.Close acForm, Me.Name
Exit Sub
End If
'populate new recordset with data from table in previous form
Do Until RS.EOF
'putting the data from the "old" recordset into the new one, shortened again, you get the idea
cpRS.AddNew
cpRS.Fields("some_value") = RS("some_value")
cpRS.Fields("some_value2") = RS("some_value2")
cpRS.Fields("first_new_field_checkbox") = False
cpRS.Fields("second_new_field_textbox") = ""
cpRS.Update
RS.MoveNext
Loop
Set Me.Recordset = cpRS
RS.Close
Set RS = Nothing
'cpRS.Close - I removed this
Set cpRS = Nothing
'error here:
Me.RecordSource = cpRS
The problem with this is that the records in the form will be empty. (not exactly empty. To be precise, it says #Name?
in every cell in one row.)
I'm not really sure what I'm doing wrong. When debugging, everything seems to work, I can see the recordset being filled with the data from my previous form. So my guess is that I'm just failing correctly assigning the new Recordset as source for the form. Likely the line Set Me.Recordset = cpRS
is where the incorrect assignment takes place, but I don't know how to fix it, if that's the (only) problem, that is.
The second part of this question has been moved to another thread for clearer structue.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…