Results 1 to 6 of 6

Thread: [RESOLVED] Grab data from MS Access and use it again in SQL!

  1. #1
    New Member
    Join Date
    Nov 09
    Posts
    3

    Question [RESOLVED] Grab data from MS Access and use it again in SQL!

    Hi folks,


    I need help using the data I retrieve from Access in SQL syntax. This is my code btw:


    f Code:
    1. Imports System.Data.OleDb
    2. Public Class Form1
    3.  
    4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5.  
    6.         Dim con As New OleDb.OleDbConnection
    7.         Dim ds As New DataSet
    8.         Dim da As OleDb.OleDbDataAdapter
    9.         Dim sql As String
    10.         Dim nilai As String
    11.  
    12.         con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Users\hadi\Desktop\SISTEM PENGUNDIAN ELEKTRONIK\SISTEM PENGUNDIAN ELEKTRONIK\Mini Projek_database.mdb"
    13.         con.Open()
    14.  
    15.         sql = "SELECT StudentID FROM Calon"
    16.  
    17.  
    18.         da = New OleDb.OleDbDataAdapter(sql, con)
    19.         da.Fill(ds, "Calon")
    20.  
    21.         nilai = ds.Tables("Calon").Rows(0).Item("StudentID")
    22.         RadioButton1.Text = nilai
    23.  
    24.         '   Dim con As New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=C:\Users\hadi\Desktop\SISTEM PENGUNDIAN ELEKTRONIK\SISTEM PENGUNDIAN ELEKTRONIK\Mini Projek_database.mdb")
    25.         Dim cmd As OleDbCommand
    26.  
    27.  
    28.  
    29.  
    30.  
    31.         If RadioButton1.Checked = True Then
    32.  
    33.             cmd = New OleDbCommand("UPDATE Calon SET Jumlah_Undi=Jumlah_Undi+1 WHERE StudentID = 'RadioButton1.text'", con)
    34.  
    35.  
    36.  
    37.  
    38.             Dim sdr As OleDbDataReader = cmd.ExecuteReader()
    39.  
    40.  
    41.         ElseIf RadioButton2.Checked = True Then
    42.  
    43.             cmd = New OleDbCommand("UPDATE Calon SET Jumlah_Undi=Jumlah_Undi+1 WHERE StudentID = '01DIP09F107'", con)
    44.  
    45.  
    46.             Dim sdr As OleDbDataReader = cmd.ExecuteReader()
    47.  
    48.         End If



    Using RadioButton1.text in the SQL syntax don't work even the RadioButton1.text hold the value of StudentID. but using StudentID value like eg: 01DIP09F107 is working good! Note that 01DIP09F107 is in my StudentID column.

    Now where did I do wrong? Thanks for any help. I'm looking at this thread http://www.vbforums.com/showthread.php?t=356711 right bow and comparing my code...
    Last edited by sg552; Nov 21st, 2009 at 01:50 PM.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 02
    Location
    Bristol, UK
    Posts
    35,562

    Re: Grab data from MS Access and use it again in SQL!

    Welcome to VBForums

    The FAQ article you linked to contains the answer, but I would recommend also reading the other article that it links to (Why should I use Parameters instead of putting values into my SQL string? ), because it not only helps with this kind of thing, but also solves lots of other issues.

  3. #3
    New Member
    Join Date
    Nov 09
    Posts
    3

    Re: Grab data from MS Access and use it again in SQL!

    thanks for your reply

    I look at the given link and I realize I don't need this declaration

    t Code:
    1. RadioButton1.Text = nilai

    this is my new code:

    Code:
        nilai = ds.Tables("Calon").Rows(0).Item("StudentID")
    
            Dim cmd As OleDbCommand
    
    
            If RadioButton1.Checked = True Then
    
                cmd = New OleDbCommand("UPDATE Calon SET Jumlah_Undi=Jumlah_Undi+1 WHERE StudentID = ' & nilai & '", con)
    I know nilai is holding the value of 01DIP09F101 because I can see it clearly when I test it: TextBox1.Text = nilai

    now I'm lost. why my Access is not updating but when I replace the & nilai & in the SQL syntax with the value 01DIP09F101 my database is updating thanks in advance.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 02
    Location
    Bristol, UK
    Posts
    35,562

    Re: Grab data from MS Access and use it again in SQL!

    Take another look at the code in the article, and compare it to what you have got - there is something very important missing (twice) in ' & nilai & '

  5. #5
    New Member
    Join Date
    Nov 09
    Posts
    3

    Thumbs up Re: Grab data from MS Access and use it again in SQL!

    hey! thank you very much for your quick reply!
    my database is updating!!!

    my working syntax:
    Code:
    cmd = New OleDbCommand("UPDATE Calon SET Jumlah_Undi=Jumlah_Undi+1 WHERE StudentID = '" & nilai & " '", con)
    I thought that ' and " works the same.

    thanks again

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 02
    Location
    Bristol, UK
    Posts
    35,562

    Re: [RESOLVED] Grab data from MS Access and use it again in SQL!

    Not quite... they both indicate String values, but ' is only within an SQL statement, and " is only within VB code.

    As far as VB is concerned, an SQL statement is just a String - so you need to build it in the same way as you would build any other string... or better still, don't build it at all (see the link I posted earlier for why it is better, and how to do it).

Posting Permissions

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