Why INserting Primary Key?

klynch0803

Registered User.
Local time
Today, 03:15
Joined
Jan 25, 2008
Messages
102
When I run this command on the Receive Inventory it inserts into the table the Primary Key instead of the Item Name.. When I select the item on the form it shows the Item Name so why wouldn’t it insert that? The order of the table the Combo Box looks in is “PrimaryKeyID” “Item”

Code:
Private Sub Command7_Click()
On Error GoTo Err_Command7_Click
    Dim strSQL As String

    Dim db As DAO.Database
    Dim rst As DAO.Recordset

Set db = CurrentDb()
Set rst = db.OpenRecordset("tdatinventoryrec", dbOpenDynaset)
Set rst2 = db.OpenRecordset("tdatPurchases", dbOpenDynaset)
    With rst
        .AddNew
        !RecDate = RecDate.Value
        !Item = Item.Value
        !Qty = QtyInserted.Value
        !Cost = Cost.Value
        .Update
    End With

    With rst2
        .AddNew
        !ChkTo = VendorName.Value
        !ChkDate = RecDate.Value
        !ChkAmt = Cost.Value
        .Update
    End With


    Set db = Nothing: Set rst = Nothing
    
    Me.RecDate = ""
    Me.Item = ""
    Me.QtyEntered = ""
    Me.Cost = ""
    Me.Combo4 = ""
    Me.VendorName = ""
    
    
    RecDate.SetFocus

Exit_Command7_Click:
    Exit Sub

Err_Command7_Click:
    MsgBox Err.Description & Err.Number
    Resume Exit_Command7_Click
    
End Sub
 
What displays in a combo box is controlled by the ColumnWidths property and the .Value property is controlled by the BoundColumn property. Look up ComboBox in VBA help for additional details.
 
What displays in a combo box is controlled by the ColumnWidths property and the .Value property is controlled by the BoundColumn property. Look up ComboBox in VBA help for additional details.


Duhh I knew i forgot to change something.. Thanks.. That fixed it by changing th bound to 2 in th eproperties of the combo...
 
Although the proper method for storing information IS to store the PK and not the text value (although there are exceptions to the rule). If something has to change (for example you had a typo) you would only have to change it in one place and not hundreds.
 
Duhh I knew i forgot to change something.. Thanks.. That fixed it by changing th bound to 2 in th eproperties of the combo...
Glad it helped but I would have to agree with Bob. Unless you have some special reason, the PK is what you want to store in the other table.
 

Users who are viewing this thread

Back
Top Bottom