Using Access, in one table, I have Social Security numbers and earnings/tax records, and in the other table I have Social's and emp id's.
I'm trying to get an output to a tablegridview, and using the following query line.

SELECT AcctNum,SUI,SDI,FICA,Medicare,State,Fed,TotWages FROM tblEarnings INNER JOIN tblPersonel On tblEarnings.SS=tblPersonel.SS

I've been informed (by VB.net) that I'm doing something wrong. Any help would be awesome.

Dani AI

Generated

If your grid fills with SELECT * FROM tblEarnings but comes back empty after the join, the connection/binding is fine. An empty result almost always means (a) a typo in a table/column name used in the join, (b) mismatched data types or formats on the join keys, or (c) no matching rows. Double‑check spellings (e.g., Personnel vs Personel), fully qualify your columns (e.AcctNum vs AcctNum), and confirm both SS fields are the same type and format (store SSNs as TEXT, consistently with or without dashes, and indexed).

To diagnose, first run the join in Access. If it still returns 0 rows, try a left join to find non‑matching keys:

SELECT e.AcctNum, e.SUI, e.SDI, e.FICA, e.Medicare, e.[State], e.Fed, e.TotWages
FROM tblEarnings AS e
LEFT JOIN tblPersonnel AS p ON e.SS = p.SS
WHERE p.SS IS NULL;

Rows returned here indicate values in tblEarnings.SS that do not exist (or do not match in format) in tblPersonnel.SS.

A few VB/Access tips:

  • Fill one DataTable and bind it; you do not need to Fill a DataSet as well.
  • Use Using blocks for OleDbConnection/OleDbDataAdapter and handle exceptions to surface SQL errors.
  • Avoid joining on PII like SSN in production; prefer a surrogate key (e.g., EmployeeID) and keep SSN separately, encrypted, and not used as a relational key.

Recommended Answers

All 6 Replies

I've been informed (by VB.net) that I'm doing something wrong.

What's the error message you are getting? You've only provided the SQL query, but that's not enough to know what the problem is.

There is no error. When I load the form, my datagridview is empty. If I change the query to something simple like

SELECT * FROM tblEarnings

then I have a nicely populated datagridview. So, I assume something in my first query is wrong.
Unfortunately I can't just do a SELECT * FROM tblEarnings INNER JOIN tblPersonel On tblEarnings.SS=tblPersonel.SS because each table has a lot of garabage I can't have.

Here is my code.

Public Class Form1


    Dim dbProvider As String = "PROVIDER=microsoft.ace.oledb.12.0;"
    Dim dbSource As String = "Data Source = C:\D&R\Data\DR.accdb"
    Dim conn As New OleDb.OleDbConnection
    Dim ds As New DataSet
    Dim da As New OleDb.OleDbDataAdapter
    Dim sql As String
    Dim query As String = "SELECT AcctNum,SUI,SDI,FICA,Medicare,State,Fed,TotWages FROM tblEarnings INNER JOIN tblPersonel On tblEarnings.SS=tblPersonel.SS"
    Dim command As New OleDb.OleDbCommand(query, conn)


    Private Sub connect()
        conn.ConnectionString = dbProvider & dbSource
        conn.Open()
        MsgBox(conn.State.ToString)
    End Sub



    Private Sub dgPopulate()
        connect()
        da.SelectCommand = command
        da.Fill(ds)
        Dim dt As New DataTable
        da.Fill(dt)

        Me.dgEmployees.DataSource = dt
        conn.Close()
    End Sub    

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        dgPopulate()
    End Sub
    End Class

try adding error handling in your code

Private Sub dgPopulate()
    connect()
    try
        da.SelectCommand = command
        da.Fill(ds)
        Dim dt As New DataTable
        da.Fill(dt)
        Me.dgEmployees.DataSource = dt
    catch ex as exception
        msgbox ex.message
    end try
    conn.Close()
End Sub 

Ok, i have a better understanding of your problem now. The good thing is that if when you tried a general SQL statement, your grid populates, that means that your VB code is good.

When you are in Access, have you tried to run this SQL query to see if it results in any records?

SELECT AcctNum,SUI,SDI,FICA,Medicare,State,Fed,TotWages FROM tblEarnings INNER JOIN tblPersonel On tblEarnings.SS=tblPersonel.SS

Maybe you have a typo on one of the table columns or with the table called 'tblPeronel'?

you Jorge sir, are a genius. I will now go beat my head with a frying pan.
My program now works great.

you Jorge sir, are a genius

I appreciate that, but its far from the truth...I'm glad I was able to help you find your issue.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.