Don Quixote
Don Quixote

Reputation: 11

WinForm VB.Net trying to search access database

I am trying to migrate my excel userform that connects to an access database, to WinForms VB.Net.

So far I've gotten rid of most of the errors, but I can't seem to display the result of my SQL query into the textbox.

Here is my code:

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim con As Object
    Dim rs As Object
    Dim db As String
    Dim sql As String

    con = CreateObject("ADODB.Connection")
    rs = CreateObject("ADODB.Recordset")
    db = "C:\Users\Dennis\Documents\Blending & Filling\Basis Olie Lossing\Base Oils.accdb"
    sql = "Select * from [Planning] where [Bestelbon] = " & Me.txtBestelbon.Text & ""

    con.Open("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & db)

    rs.Open(sql, con)

    Me.txtProduct.Text = rs(7)

    rs = Nothing
    con.Close
    con = Nothing

End Sub

Keep getting error "Conversion from type 'Field' to type 'String' is not valid"

Upvotes: 0

Views: 55

Answers (1)

Bart McEndree
Bart McEndree

Reputation: 3293

You want rs.Fields(7).Value which is a string, not rs(7) which is a field.

Upvotes: 2

Related Questions