VB2008/MS Access problems

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Montravont
    New Member
    • Sep 2008
    • 10

    VB2008/MS Access problems

    Greetings,

    I have a program setup for which I would like to pull specific data from an Access database I have created. I know the connection to the database is functioning as I can pull mass data to populate a list box. Where I am having trouble is this. I would like to select a name from said list box and have the database return only information based on the name selected. Once I pull this information I would like to then place each item from this data into it's relevant place on the form. I'd rather not do this using a datagrid on the form. This is what I have currently.

    *Note, the names being displayed in the list box is a direct match to the primary keys of each row

    Code:
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
            Dim selectedchar As String = characterlst.SelectedItem
            Dim dt As New DataTable
    
            con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Documents and Settings\Tech\My Documents\4e.mdb;"
            con.Open()
            sql = "SELECT * FROM details WHERE [Name]=" & selectedchar
            da = New OleDb.OleDbDataAdapter(sql, con)
            da.Fill(ds, "opening")
            Try
                Form1.Nametxt.Text = ds.Tables("opening").Rows(0).Item(0)
            Catch ex As Exception
                MessageBox.Show(ex.Message & " - " & ex.Source)
            End Try
    
            con.Close()
    
            Me.DialogResult = System.Windows.Forms.DialogResult.OK
            Me.Close()
        End Sub
    When I run this code it gives me an error saying
    "no value given for one or more required parameters vb access"

    If I change the select statement to read

    "SELECT * FROM details WHERE Primary Key =" & selectedchar
    it throws me an error saying the syntax is incorrect

    In both instances the error is thrown at line 8.

    Any help would be much appreciated.
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    you need to select an item from characterlst before going for the clock event of the button

    Comment

    • Montravont
      New Member
      • Sep 2008
      • 10

      #3
      Meant to update this actually. The problem was this.

      I was using

      characterlst.se lecteditem

      when I should have been using

      characterlst.te xt

      .selecteditem was returning and integer value for the list location of the selected object instead of the actual text of the selection.

      combine that with the fact that for the query to function properly it needed to be done as thus

      "Select * FROM details Where name=" & "'" & selectedchar & "'"

      the proper/working code is as follows

      Code:
      Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
              Dim selectedchar As String = characterlst.Text
              con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Documents and Settings\Tech\My Documents\4e.mdb;"
              con.Open()
              sql = "SELECT * FROM details WHERE name='" & selectedchar & "'"
              da = New OleDb.OleDbDataAdapter(sql, con)
              da.Fill(ds, "opening")
              Try
                  Form1.Nametxt.Text = ds.Tables("opening").Rows(0).Item(0)
                  Form1.Racecmb.SelectedItem = ds.Tables("opening").Rows(0).Item(1)
              Catch ex As Exception
                  MessageBox.Show(ex.Message & " - " & ex.Source)
              End Try
      
              con.Close()
              Form1.Enabled = True
              Me.DialogResult = System.Windows.Forms.DialogResult.OK
              Me.Close()
          End Sub

      Comment

      Working...