Results 1 to 19 of 19

Thread: help please!

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125

    Smile help please!

    hi everyone! this is my first time to post here and I'm glad to be a member of vbforums.com! i'm a beginner in programming and in vb.net platform...I'm having problems with comboboxes and textboxes.My problem is, i have a dbase in ms access and i bound the table productlist to my combobox which displays the productIDs. What i want to happen is that when a user selects a productID from the combobox, the other details in the productlist table corresponding to the id selected be displayed in the textboxes. I know this is simple but i just can't figure it out...there are some threads here that is somehow similar to my problem but I tried it and it didn't work...please help me! thanks in advance!

  2. #2
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    i've been playing around with the northwind database and categories table. and this is what i got. hope this helps.
    VB Code:
    1. Dim cn As New SqlConnection()
    2.    Dim da As New SqlDataAdapter()
    3.    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.       cn.ConnectionString = "user id=sa;password=password;initial catalog=northwind"
    5.       cn.Open()
    6.  
    7.       Dim cmdtext As String = "select * from categories"
    8.       da.SelectCommand = New SqlCommand(cmdtext, cn)
    9.       Dim dr As SqlDataReader = da.SelectCommand.ExecuteReader
    10.       While dr.Read
    11.          ComboBox1.Items.Add(dr.GetInt32(0).ToString)
    12.       End While
    13.       dr.Close()
    14.  
    15.       ComboBox1.SelectedIndex = 0
    16.    End Sub
    17.  
    18.    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    19.       Dim cmdtext As String = "select * from categories where categoryid=" & ComboBox1.SelectedItem.ToString
    20.       da.SelectCommand = New SqlCommand(cmdtext, cn)
    21.       Dim dr As SqlDataReader = da.SelectCommand.ExecuteReader
    22.       dr.Read()
    23.  
    24.       TextBox1.Text = dr.GetString(1)
    25.       TextBox2.Text = dr.GetString(2)
    26.       dr.Close()
    27.    End Sub
    i doubt this is not the best way. but sure for beginners like us. lol

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125
    thank you so much for replying! just wondering if you have a version for this code in ms access.=) if you don't this will also be fine...i'll look up for references to be able to convert your codes...thanks so much! take care!

  4. #4
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    That will work for an access database too, or it should...

    You just have to change the connection string...

    "Provider = Microsoft Jet OLEDB 4.0; Data Source = " & DBLocation & ";Persist Security Info = False"

    if you have a password, just change the last security part to

    Jet OLEDB:Database Password = myPassWord

    Laterz,

    Squirrelly1
    Last edited by squirrelly1; Jun 27th, 2004 at 11:06 PM.
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  5. #5
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    My bad... I didn't look at the code completely...

    Here's some sample code to connect to an Access Database:

    First, reference the ADODB ActiveX controls...

    VB Code:
    1. Dim Conn as ADODB.Connection
    2. Dim RS as ADODB.Recordset
    3.  
    4. Conn.Open ("Connection String") 'See above
    5. RS.Open (SqlString, Conn, adOpenStatic, adLockPessimistic)

    you could also do something like

    VB Code:
    1. RS = Conn.ExecuteSQL (SqlString)

    or at least i think you can

    Hope this helps some...

    I never use binding to do my data access/manipulation...

    you can just add the data to the combo box like so..

    VB Code:
    1. For i = 0 to RS.Recordcount - 1
    2.    cboCombo.Items.Add (RS.Field("index or name of field"))
    3.    RS.MoveNext
    4. Next

    Squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  6. #6
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    does sqlblah_blah work in access? i don't know that

    VB Code:
    1. Dim cn As New OleDbConnection()
    2.    Dim da As New OleDbDataAdapter()
    3.    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.       cn.ConnectionString = "provider=microsoft.jet.oledb.4.0;data source=" & Application.StartupPath & "\northwind.mdb"
    5.       cn.Open()
    6.  
    7.       Dim cmdtext As String = "select * from categories"
    8.       da.SelectCommand = New OleDbCommand(cmdtext, cn)
    9.       Dim dr As OleDbDataReader = da.SelectCommand.ExecuteReader
    10.       While dr.Read
    11.          ComboBox1.Items.Add(dr.GetInt32(0).ToString)
    12.       End While
    13.       dr.Close()
    14.  
    15.       ComboBox1.SelectedIndex = 0
    16.    End Sub
    17.  
    18.    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    19.       Dim cmdtext As String = "select * from categories where categoryid=" & ComboBox1.SelectedItem.ToString
    20.       da.SelectCommand = New OleDbCommand(cmdtext, cn)
    21.       Dim dr As OleDbDataReader = da.SelectCommand.ExecuteReader
    22.       dr.Read()
    23.  
    24.       TextBox1.Text = dr.GetString(1)
    25.       TextBox2.Text = dr.GetString(2)
    26.       dr.Close()
    27.    End Sub
    provided you have northwind.mdb in your project_folder\bin directory.

  7. #7
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    here's your northwind.mdb mate. just unzip.
    Attached Files Attached Files

  8. #8
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    If you were asking if Access supports SQL, yes it does...

    Squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  9. #9
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    sort of. i mean i want to know can i use sqlconnection() object in access and other sqlother_objects. i don't know much about this thing. is it possible? and if so, how? thanks...

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125

    Smile

    thank you very much...I'll try everything that you've said...thanks again!

  11. #11
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    IN Access, as in MS-Access forms, or WITH Access?

    If you mean WITH access, then yes, you'd use it just the same with all databases. THe only major difference would be your connection string. Do you know ADO.NET?

  12. #12
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    MS Access. can i use sqlconnection() and other sqlobjects? my wild guess is that MS Access is for oledb [and other ole provided] mysql is odbc. help me mate. i don't know much about this. thanks.

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125
    i was successful in connecting my access database but i had this error as the form is being loaded:

    An unhandled exception of type 'System.InvalidCastException' occurred in system.data.dll

    Additional information: The data value could not be converted for reasons other than sign mismatch or data overflow. For example, the data was corrupted in the data store but the row was still retrievable.

    by the way, the data that i'm trying to retrieve have currency datatypes as well as string datatypes. i hope you could still help me.thanks in advance.

  14. #14
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Originally posted by brown monkey
    MS Access. can i use sqlconnection() and other sqlobjects? my wild guess is that MS Access is for oledb [and other ole provided] mysql is odbc. help me mate. i don't know much about this. thanks.
    I don't believe that it is possible to use .NET code in MS-Access forms. I think it still uses VBA. So the answer would be no, not possible.

  15. #15
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Originally posted by siomai
    i was successful in connecting my access database but i had this error as the form is being loaded:

    An unhandled exception of type 'System.InvalidCastException' occurred in system.data.dll

    What line of code is highlighted?

    Also, put a Try...Catch block around it and show the message that comes up.

  16. #16
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    [mendhak] thanks mate.

    [siomai] somedatareader.getvalue(index).tostring(). just try to do the object getter for data reader and cast it to string. hope this helps.

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125
    [mendhak] it's on cmbID.text.add(dr.GetInt32(0).Tostring)

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125
    [brown monkey] is this a declaration? sorry i'm really new in programming. please bear with me. What i did is i searched for the format you gave me and change the getInt32(0).tostring to getvalue(0).tostring. Is that right?

  19. #19
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Why not just us a

    dr.GetString(0)

    instead? You're converting to string anyways!

    Also, make sure you check for a null value!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width