how to get data from database using combobox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rashmiraj
    New Member
    • Sep 2007
    • 11

    how to get data from database using combobox

    can anyone help me to write the code to get a field from database using combobox and that should display the corresponding record in datagrid using vb6.0
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    As you have posted a question in the articles section it is being moved to Visual Basic Forum

    MODERATOR.

    Comment

    • jrtox
      New Member
      • Sep 2007
      • 89

      #3
      Originally posted by rashmiraj
      can anyone help me to write the code to get a field from database using combobox and that should display the corresponding record in datagrid using vb6.0
      1 Question, Are you Using ADODC Object or ADODB?


      Lets Say that your using ADODC, Just download my attachment.

      1 thing, open it On your Desktop. if Error, just establish the connection again of the ADODC Object.

      Regards
      Ervin
      Attached Files

      Comment

      • rashmiraj
        New Member
        • Sep 2007
        • 11

        #4
        Originally posted by jrtox
        1 Question, Are you Using ADODC Object or ADODB?


        Lets Say that your using ADODC, Just download my attachment.

        1 thing, open it On your Desktop. if Error, just establish the connection again of the ADODC Object.

        Regards
        Ervin

        thanks a lot.......... thats what exactly i needed.....

        Comment

        • rashmiraj
          New Member
          • Sep 2007
          • 11

          #5
          Originally posted by rashmiraj
          thanks a lot.......... thats what exactly i needed.....


          but i have one more problem..if i select id(which is in autonumber)in combobox it displays the corresponding records from database.. but not for strings... how to write the code to select the string in combobox.......

          Comment

          • jrtox
            New Member
            • Sep 2007
            • 89

            #6
            Originally posted by rashmiraj
            but i have one more problem..if i select id(which is in autonumber)in combobox it displays the corresponding records from database.. but not for strings... how to write the code to select the string in combobox.......
            Lets just base to the sample i gave.

            we do have the line code as follows

            sql = "SELECT * FROM sample WHERE ID=" & Combo1.Text & ""

            That line was used to determine the "Number or Autonumber" data type

            to determine the string just put a sigle qoute( ' ) after equal ( =) sign and before last the qoute. now it will become

            sql = "SELECT * FROM sample WHERE ID='" & Combo1.Text & "'"

            Regards
            Ervin

            Comment

            • rashmiraj
              New Member
              • Sep 2007
              • 11

              #7
              Originally posted by jrtox
              Lets just base to the sample i gave.

              we do have the line code as follows

              sql = "SELECT * FROM sample WHERE ID=" & Combo1.Text & ""

              That line was used to determine the "Number or Autonumber" data type

              to determine the string just put a sigle qoute( ' ) after equal ( =) sign and before last the qoute. now it will become

              sql = "SELECT * FROM sample WHERE ID='" & Combo1.Text & "'"

              Regards
              Ervin
              i have used the code for strings but it doesn't display the string in combobox and in datagrid... i have created database in which the columns are name,Course,Cou rse duration and etc....
              i hv written code like
              sql = "SELECT * FROM mytable WHERE Name='" & Combo1.Text & "'"

              the name column in combobox and datagrid is empty.... and also the names are deleted in database....... . plz suggest me what to do for this?

              Comment

              • jrtox
                New Member
                • Sep 2007
                • 89

                #8
                Originally posted by rashmiraj
                i have used the code for strings but it doesn't display the string in combobox and in datagrid... i have created database in which the columns are name,Course,Cou rse duration and etc....
                i hv written code like
                sql = "SELECT * FROM mytable WHERE Name='" & Combo1.Text & "'"

                the name column in combobox and datagrid is empty.... and also the names are deleted in database....... . plz suggest me what to do for this?

                Hello,

                Does it Gives a Runtime or Not?
                Is there any Records in your database?

                Comment

                • rashmiraj
                  New Member
                  • Sep 2007
                  • 11

                  #9
                  Originally posted by jrtox
                  Hello,

                  Does it Gives a Runtime or Not?
                  Is there any Records in your database?

                  its not giving any runtime...recor ds are there in database & if i select "all" in combobox it displays the data except name,if i select any perticular name in combobox the whole columns in datagrid will become empty..
                  my code is :
                  Dim sql As String

                  Private Sub Combo1_Click()

                  If Combo1.Text = "All" Then
                  sql = "SELECT * FROM mytable"
                  Else
                  sql = "SELECT * FROM mytable WHERE Name= '" & Combo1.Text & "'"
                  End If

                  Adodc1.CommandT ype = adCmdText
                  Adodc1.RecordSo urce = sql
                  Adodc1.Refresh

                  Set DataGrid1.DataS ource = Adodc1

                  End Sub

                  Private Sub Form_Load()

                  'Loading Data from database to your Combo box
                  sql = "SELECT * FROM mytable"

                  'Adodc1.Refresh
                  Adodc1.CommandT ype = adCmdText
                  Adodc1.RecordSo urce = sql
                  Adodc1.Refresh

                  While Not Adodc1.Recordse t.EOF

                  Combo1.AddItem Adodc1.Recordse t("Name")

                  Adodc1.Recordse t.MoveNext
                  Wend

                  Combo1.AddItem "All"

                  Set DataGrid1.DataS ource = Adodc1


                  End Sub

                  Comment

                  • jrtox
                    New Member
                    • Sep 2007
                    • 89

                    #10
                    Originally posted by rashmiraj
                    its not giving any runtime...recor ds are there in database & if i select "all" in combobox it displays the data except name,if i select any perticular name in combobox the whole columns in datagrid will become empty..
                    my code is :
                    Dim sql As String

                    Private Sub Combo1_Click()

                    If Combo1.Text = "All" Then
                    sql = "SELECT * FROM mytable"
                    Else
                    sql = "SELECT * FROM mytable WHERE Name= '" & Combo1.Text & "'"
                    End If

                    Adodc1.CommandT ype = adCmdText
                    Adodc1.RecordSo urce = sql
                    Adodc1.Refresh

                    Set DataGrid1.DataS ource = Adodc1

                    End Sub

                    Private Sub Form_Load()

                    'Loading Data from database to your Combo box
                    sql = "SELECT * FROM mytable"

                    'Adodc1.Refresh
                    Adodc1.CommandT ype = adCmdText
                    Adodc1.RecordSo urce = sql
                    Adodc1.Refresh

                    While Not Adodc1.Recordse t.EOF

                    Combo1.AddItem Adodc1.Recordse t("Name")

                    Adodc1.Recordse t.MoveNext
                    Wend

                    Combo1.AddItem "All"

                    Set DataGrid1.DataS ource = Adodc1


                    End Sub
                    ReDownload and Run the program
                    Attached Files

                    Comment

                    • rashmiraj
                      New Member
                      • Sep 2007
                      • 11

                      #11
                      Originally posted by jrtox
                      ReDownload and Run the program
                      if i run your project it works but for my project ,for same code again am getting same problem.... when i run your project am getting an alert msg like"unable to bind to field or data member" if i click "ok",then it works.. my dos is windows xp and data base is ms access..

                      Comment

                      • jrtox
                        New Member
                        • Sep 2007
                        • 89

                        #12
                        Originally posted by rashmiraj
                        if i run your project it works but for my project ,for same code again am getting same problem.... when i run your project am getting an alert msg like"unable to bind to field or data member" if i click "ok",then it works.. my dos is windows xp and data base is ms access..

                        How could it be,

                        Attach your project, let me see it.
                        and i tell you what to do.

                        Important:

                        i will not d coding, you will be the one who apply in your coding.


                        Regards
                        Phils Ervin

                        Comment

                        • rashmiraj
                          New Member
                          • Sep 2007
                          • 11

                          #13
                          Originally posted by jrtox
                          How could it be,

                          Attach your project, let me see it.
                          and i tell you what to do.

                          Important:

                          i will not d coding, you will be the one who apply in your coding.


                          Regards
                          Phils Ervin
                          ok this is my project
                          Attached Files

                          Comment

                          • jrtox
                            New Member
                            • Sep 2007
                            • 89

                            #14
                            Originally posted by rashmiraj
                            ok this is my project


                            Hello,

                            Remove Data Source of the combo box.

                            Important:
                            Youve been encountered now a basic of VB6, I tell you to read and study the basics.


                            Regards
                            Ervin

                            Comment

                            • rashmiraj
                              New Member
                              • Sep 2007
                              • 11

                              #15
                              Originally posted by jrtox
                              Hello,

                              Remove Data Source of the combo box.

                              Important:
                              Youve been encountered now a basic of VB6, I tell you to read and study the basics.


                              Regards
                              Ervin

                              thank you somuch... its working.... am an beginner....... .

                              Comment

                              Working...