Results 1 to 16 of 16

Thread: Combo and access 2000 help

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    Combo and access 2000 help

    Hey Guys,

    I was wondering if you could help me out please.

    What i'm trying to do is load my information from my access 2000 database the table is called Product and the field is called DVDs what i want it to do is load the DVD's in to the combobox its called cboProduct and when they select a dvd it shows the cost in txtCost on my form this is my connection module code?.

    code Code:
    1. Imports System.Data.OleDb
    2.  
    3. Module modConnect
    4.  
    5.     Public myConn As New OleDbConnection
    6.     Public myCmd As New OleDbCommand
    7.     Public MyDR As OleDbDataReader
    8.     Public frUpdate As Boolean 'Holding for updating entry
    9.     Public frView As Boolean 'Holding for viewing entry
    10.  
    11.     Public Function IsConnected(ByVal strQry As String, ByVal ver As Boolean)
    12.         Try
    13.  
    14.             If myConn.State = ConnectionState.Open Then myConn.Close()
    15.             myConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\db1.mrk;User Id=admin;Jet OLEDB:Database Password=test"
    16.             myConn.Open()
    17.  
    18.             myCmd.CommandText = strQry
    19.             myCmd.Connection = myConn
    20.  
    21.             If ver = False Then
    22.                 MyDR = myCmd.ExecuteReader()
    23.             Else
    24.                 myCmd.ExecuteNonQuery()
    25.             End If
    26.  
    27.         Catch ex As Exception
    28.             MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
    29.         End Try
    30.     End Function
    31.  
    32. End Module
    come back and mark your original post as resoved if your problem is fixed

    Jamie Garland

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

    Re: Combo and access 2000 help

    Simply populate a DataTable and bind it to both controls:
    vb.net Code:
    1. With myComboBox
    2.     .DisplayMember = "Name"
    3.     .ValueMember = "ID"
    4.     .DataSource = myDataTable
    5. End With
    6.  
    7. myTextBox.DataBindings.Add("Text", myDataTable, "Cost")
    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
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    Re: Combo and access 2000 help

    Hello,

    I've tried this code you gave me but it seems to not work but it also dosent reveal any errors?. This is my full frmorders page code. The code you gave me is under form_load.

    Hope you can help please?.

    frmOrder Code:
    1. Imports System.Data.OleDb
    2.  
    3. Public Class frmOrder
    4.  
    5.     Private Sub ButtonCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCancel.Click
    6.         Me.Dispose()
    7.     End Sub
    8.  
    9.     Private Sub ButtonSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSave.Click
    10.  
    11.         If frUpdate = False Then
    12.             IsConnected("Insert into tblCustomers values(" & _
    13.                         Me.txtId.Text & ",'" & _
    14.                         Me.txtName.Text & "','" & _
    15.                         Me.txtAddress.Text & "','" & _
    16.                         Me.txtNotes.Text & "','" & _
    17.                         Me.DateGot.Text & "','" & _
    18.                         Me.PayDate.Text & "','" & _
    19.                         Me.cboOrder.Text & "','" & _
    20.                         Me.txtCost.Text & "')", True)
    21.  
    22.             MsgBox("Successfully Saved!", MsgBoxStyle.Information, "Saved")
    23.             Call LoadID()
    24.         Else
    25.  
    26.             IsConnected("Update tblCustomers set FullName='" & Me.txtName.Text & _
    27.                         "',Address='" & Me.txtAddress.Text & _
    28.                         "',Notes='" & Me.txtNotes.Text & _
    29.                         "',DateGot='" & Me.DateGot.Text & _
    30.                         "',PayDate='" & Me.PayDate.Text & _
    31.                         "',Stuff='" & Me.cboOrder.Text & _
    32.                         "',Cost='" & Me.txtCost.Text & "' where ID='" & Me.txtId.Text & "'", True)
    33.  
    34.             MsgBox("Successfully Updated!", MsgBoxStyle.Information, "Updated")
    35.  
    36.         End If
    37.  
    38.     End Sub
    39.  
    40.     Private Sub frmOrder_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
    41.         frUpdate = False
    42.         Me.Dispose()
    43.     End Sub
    44.  
    45.  
    46.     Private Sub frmOrder_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    47.         On Error GoTo err
    48.         Call LoadID()
    49.         Exit Sub
    50. err:
    51.         MsgBox(Err.Description, MsgBoxStyle.Critical, "Error")
    52.  
    53.        With cboOrder
    54.             .DisplayMember = "FullName"
    55.             .ValueMember = "ID"
    56.             .DataSource = "tblCustomers"
    57.         End With
    58.  
    59.         txtCost.DataBindings.Add("Text", "tblCustomers", "Cost")
    60.  
    61.     End Sub
    62.  
    63.     Private Sub LoadID()
    64.         If frUpdate = False Then
    65.  
    66.             txtName.Text = ""
    67.             txtAddress.Text = ""
    68.             txtNotes.Text = ""
    69.             DateGot.Text = ""
    70.             PayDate.Text = ""
    71.             cboOrder.Text = ""
    72.             txtCost.Text = ""
    73.  
    74.             IsConnected("Select count(ID) from tblCustomers", False)
    75.  
    76.             If MyDR.Read = True Then
    77.                 Me.txtId.Text = MyDR.GetValue(0) + 1
    78.             Else
    79.                 Me.txtId.Text = 1
    80.             End If
    81.             txtName.Focus()
    82.  
    83.         End If
    84.  
    85.  
    86.     End Sub
    87.  
    88.  
    89. End Class
    Last edited by Jamie_Garland; Dec 31st, 2011 at 07:14 AM.
    come back and mark your original post as resoved if your problem is fixed

    Jamie Garland

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

    Re: Combo and access 2000 help

    The code I provided works fine. You just didn't implement it properly in your own app. Have a close look at what I did and compare it to what you did. There's a big glaring difference.
    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
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    Re: Combo and access 2000 help

    This is what u did

    Code Code:
    1. With myComboBox
    2. * * .DisplayMember = "Name"
    3. * * .ValueMember = "ID"
    4. * * .DataSource = myDataTable
    5. End With
    6. *
    7. myTextBox.DataBindings.Add("Text", myDataTable, "Cost")

    And what I did I had to do as it was give things like not defined and that.
    come back and mark your original post as resoved if your problem is fixed

    Jamie Garland

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

    Re: Combo and access 2000 help

    What do you think this does?
    Code:
    With myComboBox
        .DisplayMember = "Name"
        .ValueMember = "ID"
        .DataSource = myDataTable
    End With
    
    myTextBox.DataBindings.Add("Text", myDataTable, "Cost")
    Doesn't that look like I'm assigning a DataTable to the DataSource property? What are you assigning to your DataSource property? Is it your DataTable?
    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

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    Re: Combo and access 2000 help

    I did it like this?


    Code Code:
    With cboOrder
    .DisplayMember = "FullName"
    .ValueMember = "ID"
    .DataSource = "tblcustomers"
    End With
    *
    Txtcost.DataBindings.Add("Text", tblCustomers, "Cost")
    come back and mark your original post as resoved if your problem is fixed

    Jamie Garland

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    Re: Combo and access 2000 help

    I did it like this?


    Code Code:
    1. With cboOrder
    2. .DisplayMember = "FullName"
    3. .ValueMember = "ID"
    4.  .DataSource = "tblcustomers"
    5. End With
    6. *
    7. Txtcost.DataBindings.Add("Text", tblCustomers, "Cost")[/QUOTE]
    come back and mark your original post as resoved if your problem is fixed

    Jamie Garland

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

    Re: Combo and access 2000 help

    Instead of just reposting code that you have already posted and therefore has no value at all, how about you answer my question?
    What are you assigning to your DataSource property? Is it your DataTable?
    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

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    Re: Combo and access 2000 help

    Yeah it's my DataTable from the database I'm using.
    come back and mark your original post as resoved if your problem is fixed

    Jamie Garland

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

    Re: Combo and access 2000 help

    No, it's not your DataTable. It's a String. What do double quotes mean in VB.NET? When have you ever used a variable inside double quotes?
    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

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    Re: Combo and access 2000 help

    i'm not sure thats the way i have been showing for awhile on tutorials?
    come back and mark your original post as resoved if your problem is fixed

    Jamie Garland

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

    Re: Combo and access 2000 help

    I very much doubt that. Even if it was, surely you know the difference between a String containing he name of a DataTable and the DataTable itself.
    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

  14. #14

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    Re: Combo and access 2000 help

    nope i dont im new to this and learning from tutorials?.
    come back and mark your original post as resoved if your problem is fixed

    Jamie Garland

  15. #15

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    Re: Combo and access 2000 help

    Do i have to put this at the top of the form.

    also just tried this but still dosent work?. Its also loading from a diffrent table aswell the original table is called tblCustomers but this is tblStuff.?

    vb Code:
    1. Dim tblStuff as string
    2.  
    3. With cboOrder
    4.             .DisplayMember = "NameName"
    5.             .ValueMember = "ID"
    6.             .DataSource = tblStuff
    7.         End With
    8.  
    9.         txtCost.DataBindings.Add("Name", tblStuff, "Cost")

    ?.
    Last edited by Jamie_Garland; Dec 31st, 2011 at 04:16 PM.
    come back and mark your original post as resoved if your problem is fixed

    Jamie Garland

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

    Re: Combo and access 2000 help

    You need to put the code wherever you want it to be executed. If you want it to be executed when the user clicks a Button then you put it in the Click event handler of that Button. If you want it to be executed when a form loads then you put it in Load event handler of that form. It's up to you to decide when you want the code executed and then handle the appropriate event.

    As for the code not working, that's because you haven't done it properly. Have you actually read the documentation for the members that you're using? If not, why not? Do you know what each of the parameters of the Add method mean? If not, why haven't you read the documentation to find out?
    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