visual basic and access help

Discussion in 'Software' started by Pure-D, Mar 21, 2007.

  1. Pure-D

    Pure-D Private E-2

    Hi,
    I’m having trouble querying an access database through my visual basic program. Here’s what the database looks like…

    Database name TestDB
    TestTable1 has these fields testinfo, testinfo2, name, region, number

    I have the user input the region in to a textbox, something like UK or USA. Then when they click query, it will pull up the region matching the user input and display name, number, testinfo1 from that table in to a multiline textbox. So it could look like this…

    UK, bob, 9090, blah
    UK, Joe, 8998, wooo
    UK, Bill, 9876, maaa

    I’m doing that because later I’m gonna try and export whats in the text box to a text file. The code I’ve done for the query doesn’t seem to work, here it is…

    Code:
    Imports System.IO
    Imports System.Data
    Public Class QueryTest
        Dim con As New OleDb.OleDbConnection
        Dim ds As New DataSet
        Dim da As OleDb.OleDbDataAdapter
        Dim sql As String
        Dim myString As String
    
        Private Sub QueryTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    
    
        Private Sub butQuery_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butQuery.Click
            myString = txtRegion.ToString()
            Dim cs As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\TestStuff\VB\TestDB.mdb" 
            Dim cn As New OleDb.OleDbConnection(cs
            Dim dt As New DataTable
            Dim cmd As New OleDb.OleDbCommand("SELECT name, number, testInfo1 FROM TestTable1 WHERE Region = “& myString, cn)
            cmd.Parameters.Add("@myString", OleDb.OleDbType.VarChar, 30).Value = myString
            Dim da As New OleDb.OleDbDataAdapter(cmd)
    
    
            
            da.Fill(dt)
    
            txtQuery.Text = dt.Rows(0)("Person")
            
        End Sub
    End Class 
    I’ve tried everything to get it working but it wont.
    It gives me this error at the da.Fill(dt) part…
    Syntax error (missing operator) in query expression 'Region = & myString'.

    Can anyone help me get it working? Also I’m not quite sure how to get all the information to show in the multiline text box. Anyhelp would be great.

    Thanks,
    Jack
     
  2. Kodo

    Kodo SNATCHSQUATCH

    Dim cmd As New OleDb.OleDbCommand("SELECT name, number, testInfo1 FROM TestTable1 WHERE Region = ?", cn)

    Try that..
    You had the parameter as part of the query.. and you were already adding a parameter value to the parameter collection of the cmd object.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds