Excel vba command to retrieve specific information from multiple access table

william2306

New Member
Joined
Jul 14, 2016
Messages
1
Hello,
I have a problem with importing data from ms access to excel with <acronym title="visual basic for applications">VBA</acronym>. I use "ADODB" to connect excel with acces.
I have a few tables in ms access. I want to get the ClientDesc and ProductDesc when I input on TrackNo and with a command button to run the VBA. I tried it with inner join (incomplete, without ProductTB), but i only get the first row of information.
so how can i get all the respective/ required info (ClientDesc and ProductDesc).
Thank you for your help.


below is my current code in excel module.
Code:
Option Explicit
Public Const DBLink As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Users\USER\Desktop\Mother.accdb;"
Public Sub SearchTRK(TrackNo As String)
  
    Dim CN As ADODB.Connection
    Dim Rs As ADODB.Recordset    
    Set Rs = New ADODB.Recordset   
    
    Set CN = New ADODB.Connection
    CN.Open DBLink
      
     Set Rs = CN.Execute("select MotherTb.Customer, ClientTb.ClientDesc from MotherTb inner join ClientTb on Mother.Customer = ClientTb.ClientID")
    
     If Not Rs.EOF Then
        Cells(2, 2) = Rs("ClientDesc")
    Else
        MsgBox "Record not found"
    End If
    
    Set Rs = Nothing
    
End Sub

current code in excel sheet
Code:
Private Sub Search1_Click()
SearchTRK (Cells(1, 2))
End Sub


Example of table in access
MotherTb
TrackNoCustomerProduct
2017-00111
2017-00212
2017-00323
2017-00414

<tbody>
</tbody>



ClientTb
ClientIDClientDesc
1AAA
2BBB

<tbody>
</tbody>

ProductTb
ProductIDProductDesc
1Item A
2Item B
3Item C
4Item D

<tbody>
</tbody>


Example in Excel (End-product)
TrackNo2017-003 (input)
ClientBBB (output)
ProductItem C (output)

<tbody>
</tbody>
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

Forum statistics

Threads
1,214,948
Messages
6,122,420
Members
449,083
Latest member
Ava19

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top