CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2001
    Posts
    5

    How use data from a field in an SQL string?

    Hello folks;

    I would like to know how I can insert the value of a field on my form in an SQL string.
    For example:
    There is a field on my form with the name txtKey; it contains a number.
    I would like to make a query which uses the current number in that field to search in a database:
    "SELECT Name, IDNumber FROM People
    where IDNumber = txtKey"
    This doesn't work, I know, but how can I do this?
    How can I use the current value of a field in this Query?

    Thanks in advance for any reply;
    Carl




  2. #2
    Join Date
    Mar 2000
    Location
    Sheffield, UK
    Posts
    52

    Re: How use data from a field in an SQL string?

    try:

    strSql = "SELECT Name, IDNumber FROM People WHERE IDNumber = " & txtKey.text

    Cheers

    Rob


  3. #3
    Join Date
    May 2001
    Posts
    5

    Re: How use data from a field in an SQL string?

    Thanks a lot Rob!

    Your Query
    strSql = "SELECT Name, IDNumber FROM People WHERE IDNumber = " & txtKey.text
    works perfectly.

    Now, if anybody can explain me how to put the result from this query in a Datagrid. I Can't find out how to connect a Datagrid to my connection.
    I am using the following connection for this query:
    Dim cnn2 As New ADODB.Connection
    Dim cmd2 As New ADODB.Command
    Dim rs2 As New ADODB.Recordset
    Once I know how to do this, I can finish my program.

    Thanks you many times in advance.
    Carl
    Brussels, Belgium



  4. #4
    Join Date
    May 2001
    Posts
    7

    Re: How use data from a field in an SQL string?

    The answer is

    "SELECT Name, IDNumber FROM People
    where IDNumber =" & val(txtKey)



  5. #5
    Join Date
    May 2001
    Posts
    2

    Re: How use data from a field in an SQL string?

    Assuming ( frmMainScreen ) is the name of the form and ( msgFGInvDet ) is msFlexGrid control in that
    form and I'am using RDO resultset (make respective changes to resultset only, other things remain same )
    ShowRecSet is the name of the Resultset
    Execquery is just a command text
    glbCON is connection name

    Code starts from here below:-

    ' this string format can set in design time as well here I'am setting it at run time ok.

    frmMainScreen.msgFGInvDet.FormatString = "DNP" & _
    "|Clear Date |<Date " & _
    "|<Club |<Payment Number " & _
    "|>Amount |<Bank " & _
    "|<Payment Type |<Payment Information|<Currency Code |<Batch No. |<Comment "

    With frmMainScreen.msgFGInvDet

    .Rows = 1

    Execquery = " Use TestDbase SELECT * FROM CuMe_Curr_Pment "

    Set ShowRecSet = glbCON.OpenResultset(Name:=Execquery, _
    Type:=rdOpenForwardOnly, LockType:=rdConcurReadOnly, _
    Options:=rdExecDirect)

    Do Until ShowRecSet.EOF
    .Rows = .Rows + 1
    If ShowRecSet!Pment_Not_Process = 1 Then
    .TextMatrix(.Rows - 1, 0) = "X" ' This is if U want to display something else in the grid based on the result of resultset
    .row = .Rows - 1: .Col = 0: .CellFontBold = True
    .CellForeColor = vbRed
    Else
    .TextMatrix(.Rows - 1, 0) = "-"
    End If

    If Trim(ShowRecSet ! Pment_Status) = "PRBLM" Then
    .TextMatrix(.Rows - 1, 1) =Trim(ShowRecSet!Pment_Status)
    else: .TextMatrix(.Rows - 1, 1) ="X"
    .row = .Rows - 1: .Col = 1: .CellFontBold = True
    .CellForeColor = vbRed:


    .TextMatrix(.Rows - 1, 9) = ShowRecSet ! Pment_authoriz
    .TextMatrix(.Rows - 1, 10) = ShowRecSet ! Pment_Curr_Code
    .TextMatrix(.Rows - 1, 11) = ShowRecSet ! Pment_Batch_Claim
    .TextMatrix(.Rows - 1, 12) = ShowRecSet ! Pment_Info

    ShowRecSet.MoveNext
    Loop
    ShowRecSet.Close

    End With


    here ends the code...

    do this till Ur resultset columns or the grid columns are over.


  6. #6
    Join Date
    Apr 2001
    Location
    USA
    Posts
    161

    Re: How use data from a field in an SQL string?

    try :
    "SELECT Name, IDNumber FROM People
    where IDNumber = " & txtKey.text
    i sounds better
    Hope this is the answerd

    ---------------------------------------------
    Sure i got it... what s a form again?

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

Featured