admin管理员组

文章数量:1308118

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"

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"

Share Improve this question edited Feb 3 at 20:00 Don Quixote asked Feb 3 at 19:57 Don QuixoteDon Quixote 111 silver badge3 bronze badges 7
  • This might help stackoverflow/questions/57326820/… – Bart McEndree Commented Feb 3 at 19:59
  • Is there a particular reason that you're using ADO instead of ADO.NET? – It all makes cents Commented Feb 3 at 20:06
  • The following may be of interest: Create a simple data application by using ADO.NET, stackoverflow/a/77949790/10024425, stackoverflow/a/70484316/10024425 – It all makes cents Commented Feb 3 at 20:17
  • I simply copy/pasted my code over from vba excel to winforms vb – Don Quixote Commented Feb 3 at 20:17
  • Didn't even know there was an ado version – Don Quixote Commented Feb 3 at 20:17
 |  Show 2 more comments

1 Answer 1

Reset to default 2

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

本文标签: WinForm VBNet trying to search access databaseStack Overflow