csriram_leaf
asked on
Adding new records to a VB 6 DB Grid Control with no data to start with
Hi,
I have been struggling to add a new record to a DB grid in VB 6. I did set the AllowAddNew property to true, here is my code:
Private Sub BindDetailFields()
Dim sSQLDetail As String
sSQLDetail = " SELECT b.PONumber, b.TiresOrEquipment, b.UnitNumber, b.ItemNbr, b.ItemDesc, "
sSQLDetail = sSQLDetail & " b.QuantityOrdered, b.OrigUnitCost, b.OrigExtendedCost "
sSQLDetail = sSQLDetail & " FROM " & strCustomDB & "..PORDetail b "
sSQLDetail = sSQLDetail & " WHERE PONumber = '" & txtPONumber.Text & "'"
rsPODetail.Open sSQLDetail, cnGPCustom, adOpenDynamic, adLockOptimistic
If rsPODetail.EOF = False And rsPODetail.RecordCount > 0 Then
rsPODetail.MoveFirst
End If
If blnAddNew = True Then
With rsPODetail
.AddNew
.Fields("PONumber") = txtPONumber.Text
.Fields("TiresOrEquipment" ) = " "
.Fields("UnitNumber") = " "
.Fields("ItemNbr") = " "
.Fields("ItemDesc") = " "
.Update
.MoveFirst
End With
rsPoDetail.Requery
End If
DataGridPODetail.AllowAddN ew = True
Set DataGridPODetail.DataSourc e = rsPODetail
End Sub
The Boolean variable blnAddNew is a private form level variable that is set to True when a user clicks on the Add New button on the form. The txtPONumber.Text is dynamically generated with a formula and hasTo start with, the recordset does not have any records.
I have been struggling to add a new record to a DB grid in VB 6. I did set the AllowAddNew property to true, here is my code:
Private Sub BindDetailFields()
Dim sSQLDetail As String
sSQLDetail = " SELECT b.PONumber, b.TiresOrEquipment, b.UnitNumber, b.ItemNbr, b.ItemDesc, "
sSQLDetail = sSQLDetail & " b.QuantityOrdered, b.OrigUnitCost, b.OrigExtendedCost "
sSQLDetail = sSQLDetail & " FROM " & strCustomDB & "..PORDetail b "
sSQLDetail = sSQLDetail & " WHERE PONumber = '" & txtPONumber.Text & "'"
rsPODetail.Open sSQLDetail, cnGPCustom, adOpenDynamic, adLockOptimistic
If rsPODetail.EOF = False And rsPODetail.RecordCount > 0 Then
rsPODetail.MoveFirst
End If
If blnAddNew = True Then
With rsPODetail
.AddNew
.Fields("PONumber") = txtPONumber.Text
.Fields("TiresOrEquipment"
.Fields("UnitNumber") = " "
.Fields("ItemNbr") = " "
.Fields("ItemDesc") = " "
.Update
.MoveFirst
End With
rsPoDetail.Requery
End If
DataGridPODetail.AllowAddN
Set DataGridPODetail.DataSourc
End Sub
The Boolean variable blnAddNew is a private form level variable that is set to True when a user clicks on the Add New button on the form. The txtPONumber.Text is dynamically generated with a formula and hasTo start with, the recordset does not have any records.
ASKER
No, that was not it. There is no Refresh property available for the ADODB recordset through Intellisense. Maybe it is a hidden property. But my underlying problem was that the data grid control would not bind to a dynamic cursor and kept giving me errors, there were no errors with rsPODetail.Requery statement.
When I changed the statement
rsPODetail.Open sSQLDetail, cnGPCustom, adOpenDynamic, adLockOptimistic
to
rsPODetail.Open sSQLDetail, cnGPCustom, adOpenStatic, adLockOptimistic
and then added the new record to the recordset and bound the data grid control, the data grid control was able to display the new record without any errors
Now, I just need to figure out a way to retrieve the column index when I click on any cell in the grid. Let me know if you can help me with that.
Thanks for your time.
Chad
When I changed the statement
rsPODetail.Open sSQLDetail, cnGPCustom, adOpenDynamic, adLockOptimistic
to
rsPODetail.Open sSQLDetail, cnGPCustom, adOpenStatic, adLockOptimistic
and then added the new record to the recordset and bound the data grid control, the data grid control was able to display the new record without any errors
Now, I just need to figure out a way to retrieve the column index when I click on any cell in the grid. Let me know if you can help me with that.
Thanks for your time.
Chad
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I believe I have a good understanding of your question now. Forgive me if I am wrong, but it sounds like your rsPoDetail.Requery should be rsPoDetail.Refresh. Let me know if that helps please.
JRockSolid