Results 1 to 7 of 7

Thread: [RESOLVED] Selecting field from database and showing it on a button (MS Access)

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    108

    [RESOLVED] Selecting field from database and showing it on a button (MS Access)

    I got table1 with the columns ID, Name and IP. Can I fetch a name from the database (depending on ID) and show it as the text on a button when the form loads?

    Something like

    VB Code:
    1. Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source= " & System.AppDomain.CurrentDomain.BaseDirectory & "\db1.mdb"
    2.         Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString)
    3.  
    4.         Dim queryString As String = "SELECT Name FROM table WHERE ID = '2'"
    5.         Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
    6.         dbCommand.CommandText = queryString
    7.         dbCommand.Connection = dbConnection
    8.  
    9.         Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter
    10.         dataAdapter.SelectCommand = dbCommand
    11.         Dim dataSet As System.Data.DataSet = New System.Data.DataSet
    12.         buttonConf1.Text = dataSet.ToString

    which sets the text on the button to System.Data.DataSet...

    so how should it all actually look like?

    if the code is completely wrong could you tell me how it should be please
    Last edited by Darkstorm; Jan 14th, 2006 at 03:22 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Selecting specific field from database and showing it on a button (MS Access)

    A DataSet contains DataTables,a DataTable contains DataRows and a DataRow contains fields in which the actual data is stored. You've got three issues.

    1. You haven't executed the SQL statement so you haven't retrieved any data.
    2. You want to display a single field value, so you have to get it from within the DataSet using the relationships I mentioned before.
    3. You shouldn't use a DataAdapter to get a single value. You should just create a Command and call ExecuteScalar.
    VB Code:
    1. Dim command As New OleDbCommand(query, connection)
    2.  
    3. connection.Open()
    4. myButton.Text = CStr(command.ExecuteScalar())
    5. connection.Close()
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    108

    Re: Selecting specific field from database and showing it on a button (MS Access)

    Ok, thanks

    Now I got this code

    VB Code:
    1. Dim myQuery As String
    2.         Dim myConnection As New OleDb.OleDbConnection
    3.         myQuery = "SELECT Name FROM table WHERE ID = 2"
    4. myConnection = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source= " & System.AppDomain.CurrentDomain.BaseDirectory & "\db1.mdb"
    5. '^-- doesnt work
    6.         Dim myCommand As New OleDb.OleDbCommand(myQuery, myConnection)
    7.         myCommand.Connection.Open()
    8.         button1.Text = CStr(myCommand.ExecuteScalar())
    9.         myConnection.Close()

    doesn't work though How should I make the connection?


    Edit: heh should ofcourse be
    myConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source= " & System.AppDomain.CurrentDomain.BaseDirectory & "\db1.mdb"

    Rating up for you
    Last edited by Darkstorm; Jan 13th, 2006 at 08:22 AM.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Selecting specific field from database and showing it on a button (MS Access)

    Do you realise that many of these variables you are declaring have constructors that take arguments, and you can declare and initialise a variable on a single line, e.g.
    VB Code:
    1. Dim myQuery As String
    2. myQuery = "SELECT Name FROM table WHERE ID = 2"
    becomes
    VB Code:
    1. Dim myQuery As String = "SELECT Name FROM table WHERE ID = 2"
    and
    VB Code:
    1. Dim myConnection As New OleDb.OleDbConnection
    2. myConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source= " & System.AppDomain.CurrentDomain.BaseDirectory & "\db1.mdb"
    becomes
    VB Code:
    1. Dim myConnection As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source= " & System.AppDomain.CurrentDomain.BaseDirectory & "\db1.mdb")
    There is nothing technically wrong with what you are doing but, in my opinion, such long-winded code is actually harder to read.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    108

    Re: Selecting specific field from database and showing it on a button (MS Access)

    That'll shorten up my code a bit Thanks

  6. #6
    Fanatic Member jcavard's Avatar
    Join Date
    Jul 2005
    Location
    Quebec, CANADA
    Posts
    534

    Re: Selecting specific field from database and showing it on a button (MS Access)

    if you refer more than once to the database, you should save the connection string into a STRING variable like __CONNECTION_STRING and call this instead of long text "Provider=Microsoft.Jet.OLED..... " each time.
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

    accoustic emo-rock band: a tailormade fable

    Visual Studio 2003 / Framework 1.1

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Selecting specific field from database and showing it on a button (MS Access)

    Quote Originally Posted by jcavard
    if you refer more than once to the database, you should save the connection string into a STRING variable like __CONNECTION_STRING and call this instead of long text "Provider=Microsoft.Jet.OLED..... " each time.
    Actually, you should really only have a single Connection object. Declare the Connection object as a class-level variable. Create it in the Load event handler of the form, or wherever else is convenient if it's not in a form, which is the one place you would need the ConnectionString, then simply Open and Close it as needed.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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