How to get records by using foreign key in one table using primery key another table in visual basic 6.0

这一生的挚爱 提交于 2021-01-07 02:16:48

问题


i have two tables in same database:

  • table1 contains order_no(primery key) and cust_name
  • table2 contains order_no(foreign key) (which also have duplicates entry per food item ordered in single order), table_no, items, date, cust_name and so on...

so my question is how can i show the records in listbox and datagrid or in textbox using table1's primery key order_no so i can get all the records which have assigned the foreign key with same order number in table2

i am using visual basic 6.0


回答1:


Try something like this

Private Sub mLoadData(lOrder_no As Long)
    ' add a reference to Microsoft ActiveX Data Objects 2.8 Library
    ' add a MSHFLXGD (Microsoft Hierarchical FlexGrid) control named grData to form
    Dim rc As ADODB.Recordset
    Dim db As New ADODB.Connection
    Dim sConnString As String, sSQL As String

    'sConnString = create a connection string according to your database - https://www.connectionstrings.com/
    db.Open sConnString

    sSQL = "SELECT * FROM table2 WHERE order_no =" & lOrder_no
    Set rc = db.Execute(sSQL)
    Set grData.DataSource = rc

End Sub


来源:https://stackoverflow.com/questions/58236410/how-to-get-records-by-using-foreign-key-in-one-table-using-primery-key-another-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!