Chip Datamaker
Chip Datamaker

Reputation: 29

Referencing data held in an Access Database with Visual Basic

My project is a small Access database with two tables. The first table is "EMPLOYEES" and the second table is "TOOLS". The database keeps track of what specific tools each employee has in his/her possession. My Visual Basic form adds and deletes employees from the database via the dataset. What I would like to do is set a variable to hold the Employees full name taken from two different fields. (The "FirstName" field and the "LastName" field)
How do I reference (or get the value of) the "FIRST_NAME" and "LAST_NAME" data stored in the "EMPLOYEE" table so that I can set my Visual Basic string Variable with those two values. Is this a job for SQL?

Something like this:

     For i As int32 = 0 To myDATABASE.EMPLOYEES.column.Length

        Dim fullName As String = _
                         myDATABASE.EMPLOYEES.row(i).FIRST_NAME _
                         & " " & _
                         MyDATABASE.EMPLOYEES.row(i).LAST_NAME

        mylistBox.add("The full name is: " & fullName)

     Next

Upvotes: 0

Views: 206

Answers (2)

Joe Uhren
Joe Uhren

Reputation: 1372

You can concatenate the names in sql as Wayne G. Dunn suggests or you can also do it in vb like this:

    For i As Int32 = 0 To MyDATABASE.Tables("EMPLOYEES").Rows.Count - 1
        Dim fullName As String = _
                       MyDATABASE.Tables("EMPLOYEES").Rows(i).Item("FIRST_NAME").ToString() _
                         & " " & _
                         MyDATABASE.Tables("EMPLOYEES").Rows(i).Item("LAST_NAME").ToString()

        mylistBox.add("The full name is: " & fullName)
    Next

For something as simple as putting two strings together it's really a matter of personal preference and it's good to know both ways.

Upvotes: 0

Wayne G. Dunn
Wayne G. Dunn

Reputation: 4312

If you are trying to populate a listbox with names, then you can set the recordsource to something like:

Select [First_Name] & " " & [Last_Name] as FullName from Employees

Upvotes: 1

Related Questions