Wrox Programmer Forums
|
VB Databases Basics Beginning-level VB coding questions specific to using VB with databases. Issues not specific to database use will be redirected to other forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB Databases Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 23rd, 2006, 06:29 AM
Authorized User
 
Join Date: Aug 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default VB6 - Is this syntax correct??

rsRecordSet.Source = "Select * From tsrtime where date =('" &dt " and agentid= "&id"')

hi peeps iam trying to retrieve data from sqlserver ,
based on two values in 2 variables(one is date and the other is agent id) .using a record set.. is this syntax correct?? and any ideas to overcome date value conflict from frontend to backend.[mismatch etc]

Thankx in advance..

 
Old August 23rd, 2006, 11:27 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

This syntax is not correct; dates are not strings.
In Access SQL you should have
Code:
    rsRecordSet.Source = "SELECT *                      " & _
                         "FROM   tsrtime                " & _
                         "WHERE  date    = #" & dt & "# " & _
                         "  AND  agentid  = " & id
                         That is, unless agentid is a string, in which case you would need
Code:
                         " . . . " & _
Code:
                         "  AND  agentid = '" & id & "'"
                         In Oracle, there is no date delimiting. You have to use a conversion function (whose name I don't recall) something like:
Code:
                         "WHERE  date = TODATE('" & dt & "', 'MM-DD-YY')" & _
Code:
                         " . . . "
 
Old August 24th, 2006, 03:35 AM
Authorized User
 
Join Date: Aug 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi mate
Thanks for your response ,i have been making changes on this since i got this error the current status of my application is given below ,wot iam tryin to do is to edit an existing record which is in the database(sqlserver) ,i retrieve the record based on the date and the userid which populate from the database say user 001 on date 01/04/06 its only picks up wots in the database ,if there is any record it populates into the textboxes orelse throws a message.
i have one save buton which will update changed values..please suggest.thanks a lot for ur time and patience.

---------------------------------
Option Explicit
Private WithEvents connConnection As ADODB.Connection
Private WithEvents cno As ADODB.Connection
'Private WithEvents cnn As ADODB.Connection
Private WithEvents rsRecordSet As ADODB.Recordset
Private WithEvents rs As ADODB.Recordset
Dim i As Long
Dim j As Integer
Public Sub Cmdret_Click()
Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Set cno = New ADODB.Connection
    Set rs = New ADODB.Recordset
    cno.ConnectionString = "driver = {sqlserver};Data Source = SQLDSN ;UserId=reddy;Password=reddy04"
    cno.Open "sqldsn"

   If Listdt.Selected(i) = True And List1.Selected(i) = True Then
    rs.Open "select * from tsrtime where date='" & Listdt.List(i) & " ' and agentid = '" & List1.List(i) & " '", cno, adOpenStatic, adLockPessimistic

    Text1.Text = rs.Fields("starttime")
    Text2.Text = rs.Fields("endtime")
    Text3.Text = rs.Fields("breaklength")
    Text4.Text = rs.Fields("reasoncode")
    Text5.Text = rs.Fields("paycode")

   Else
   MsgBox "NO RECORDS FOUND FOR THE USER" + List1.List(i) + "On " + Listdt.List(i), vbExclamation, vbOK

   End If

   'While rs.EOF = False
   'If rs.EOF = True Then
      rs.MoveNext

    'rs.MoveNext

   'Wend
   'End If
   'Next

Text1.SetFocus
List1.Enabled = False
Listdt.Enabled = False

End Sub

Private Sub Form_Load()
FillCombo
End Sub

Private Sub cmdExit_Click()
cno.Close
'cno = Nothing
End
End Sub

Private Sub WriteDataFromControls()
    rs.Fields("starttime").Value = Text1.Text
    rs.Fields("endtime").Value = Text2.Text
    rs.Fields("breaklength").Value = Text3.Text
    rs.Fields("reasoncode").Value = Text4.Text
    rs.Fields("paycode").Value = Text5.Text
    rs.Update

End Sub
Private Sub cmdSave_Click()

     'MsgBox " Connection" + cno.ConnectionString
     'Dim strsql2 As String

     'If Listdt.Selected(i) = True And List1.Selected(i) = True Then
     'strsql2 = "INSERT INTO tsrtime VALUES " _
     ' & "('" & Text1.Text & ",'" & Text2.Text & "','" & Text3.Text & "','" & Text4.Text & "','" & Text5.Text & "')"
     ' cno.Open "sqldsn"
     ' cno.Execute strsql2
     ' End If


    WriteDataFromControls
    MsgBox " 1 row updated "
'rs.Update
End Sub

Private Sub FillCombo()
    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim intLoop As Integer
    Set cn = New ADODB.Connection
    Set rs = New ADODB.Recordset


    cn.ConnectionString = "driver = {sqlserver};Data Source = SQLDSN ;UserId=reddy;Password=reddy04"
    cn.Open "sqldsn"

      rs.Open "select date,agentid from tsrtime", cn, adOpenForwardOnly

    While rs.EOF = False

        Listdt.AddItem (rs.Fields("date").Value)
        List1.AddItem (rs.Fields("agentid").Value)
        rs.MoveNext
    Wend

    If rs.State = adStateOpen Then
        rs.Close
    End If
    Set rs = Nothing
    If cn.State = adStateOpen Then
        cn.Close
    End If
    Set cn = Nothing
End Sub



 
Old August 24th, 2006, 05:10 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Code:
Option Explicit
Private WithEvents cno            As ADODB.Connection
Private WithEvents rs As ADODB.Recordset
Dim i As Long
Dim j As Integer

Private Sub Form_Load()
    ' This would ba a good place to open the connection.
    FillCombo
End Sub

Private Sub FillCombo()

    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim intLoop As Integer
    Set cn = New ADODB.Connection
    Set rs = New ADODB.Recordset

    ' There’s no need to create a new connection here.  Just use cno, which you
    ' have defined with module-scope.  Test for whether it is open;  if not use
     ' cno.open. Otherwise, just use it.  One connection is all you need.
    cn.ConnectionString = "driver = {sqlserver};" & _
                          "Data Source = SQLDSN;" & _
                          "UserId=reddy;" & _
                          "Password=reddy04"
    cn.Open "sqldsn"

    rs.Open "SELECT date, agentid " & _
            "FROM   tsrtime       ", _
            cn, adOpenForwardOnly

    ' While / Wend is considered outdated.   Use Do Until rs.EOF
    '                                            Loop      instead
    While rs.EOF = False
        Listdt.AddItem (rs.Fields("date").Value)
        List1.AddItem (rs.Fields("agentid").Value)
        rs.MoveNext
    Wend

    ' I recommend single-statement Ifs in cases like this:
     If rs.State = adStateOpen Then rs.Close
    Set rs = Nothing

     If cn.State = adStateOpen Then cn.Close
    Set cn = Nothing

End Sub

Public Sub Cmdret_Click()

    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Set cno = New ADODB.Connection
    Set rs  = New ADODB.Recordset
    cno.ConnectionString = "driver = {sqlserver};" & _
                           "Data Source = SQLDSN;" & _
                           "UserId=reddy;" & _
                           "Password=reddy04"
    cno.Open "sqldsn"

    ' What is ‘i’ here?  Where does its value come from?
    ' As far as I can tell, it is never set, so it = 0 . . .
    If Listdt.Selected(i) = True And List1.Selected(i) = True Then
        ' You have an extraneous space in two locations here:
        '     Listdt.List(i) & " ' "
        '                       ¯
        ' and 
        '     List1.List(i) & " '"
        '                      ¯
        rs.Open "SELECT * " & _
                "FROM tsrtime " & _
                "WHERE date='" & Listdt.List(i) & " ' " & _
                "AND agentid = '" & List1.List(i) & " '", _
                cno, adOpenStatic, adLockPessimistic

        ' This is an awkward syntax.  I suggest:
        '            rs!starttime, and so on.
        Text1.Text = rs.Fields("starttime")
        Text2.Text = rs.Fields("endtime")
        Text3.Text = rs.Fields("breaklength")
        Text4.Text = rs.Fields("reasoncode")
        Text5.Text = rs.Fields("paycode")

    Else
        ' Using all caps gives the impression that you are shouting.
        MsgBox "NO RECORDS FOUND FOR THE USER" + List1.List(i) + "On " + Listdt.List(i), _
               vbExclamation, vbOK  ' You have a messagebox constant (vbOK) as the title here . . .
    End If

   'While rs.EOF = False
   'If rs.EOF = True Then
      rs.MoveNext   ' Why are you repositioning the recordset?

    'rs.MoveNext

   'Wend
   'End If
   'Next

    Text1.SetFocus
    List1.Enabled = False
    Listdt.Enabled = False

End Sub

Private Sub cmdExit_Click()
    cno.Close
    'cno = Nothing
    End
End Sub

Private Sub WriteDataFromControls()
    ' You declared a module scope recordset ‘rs,’ which permits this
    ' routine to finction—it knows about a recordset called ‘rs.’
    ' But everyplace except here that you use ‘rs,’ you create a 
    ' procedure-scope Recordset of that name first.  The result
    ' is that though the name is the same, that name refers to 
    ' entirely different objects.  If you prefaced your module-
    ' scope variables with ‘m,’ this wouldn’t happen . . .

    rs.Fields("starttime").Value = Text1.Text
    rs.Fields("endtime").Value = Text2.Text
    rs.Fields("breaklength").Value = Text3.Text
    rs.Fields("reasoncode").Value = Text4.Text
    rs.Fields("paycode").Value = Text5.Text
    rs.Update
End Sub

Private Sub cmdSave_Click()

    'MsgBox " Connection" + cno.ConnectionString
    'Dim strsql2 As String

    'If Listdt.Selected(i) = True And List1.Selected(i) = True Then
    'strsql2 = "INSERT INTO tsrtime " & _
    '          "VALUES ('" & Text1.Text & ",'" & Text2.Text & "','" & Text3.Text & "','" & Text4.Text & "','" & Text5.Text & "')"
     '  cno.Open "sqldsn"
     '  cno.Execute strsql2
     ' End If


    WriteDataFromControls
    MsgBox " 1 row updated "
'rs.Update
End Sub

—————————————————————————————————————————————————————————————

Option Explicit
Private mCno As ADODB.Connection
Private mRs  As ADODB.Recordset

Private Sub Form_Load()

    Set mCno = New ADODB.Connection
    Set mRs = New ADODB.Recordset

    With mCno
        .ConnectionString = "driver = {sqlserver};" & _
                            "Data Source = SQLDSN;" & _
                            "UserId=reddy;" & _
                            "Password=reddy04"
        .Open
    End With

    With mRs
        .Open "SELECT date, agentid " & _
              "FROM   tsrtime       ", _
              mCno, adOpenForwardOnly

        Do Until .EOF
            Listdt.AddItem !date
            List1.AddItem !agentid
            mRs.MoveNext
        Loop

         If .State = adStateOpen Then .Close
    End With
    Set mRs = Nothing

End Sub

Public Sub Cmdret_Click()

    With mCno
        If .State = adStateClosed Then
            .ConnectionString = "driver = {sqlserver};" & _
                                "Data Source = SQLDSN;" & _
                                "UserId=reddy;" & _
                                "Password=reddy04"
            .Open
        End If
    End With

    With mRs
        .Open "SELECT *                                         " & _
              "FROM   tsrtime                                   " & _
              "WHERE  date    = '" & Listdt.SelectedItem() & "' " & _
              "  AND  agentid = '" & List1.SelectedItem() & "'  ", _
              mCno, adOpenStatic, adLockPessimistic

        If .EOF Then
            MsgBox "No records found for user " & List1.List(i) & " On " & Listdt.List(i), _
                   VbExclamation Or vbOK, “No Record Found”
        Else
            Text1.Text = !starttime
            Text2.Text = !endtime
            Text3.Text = !breaklength
            Text4.Text = !reasoncode
            Text5.Text = !paycode
        End If
        .Close
    End With

    Text1.SetFocus
    List1.Enabled = False
    Listdt.Enabled = False

End Sub

Private Sub cmdSave_Click()

    MCno.Execute "UPDATE tsrtime (starttime, endtime, breaklength, reasoncode, paycode)    " & _
                 "VALUES ('" & Text1.Text & "', '" & Text2.Text & ', '" & Text3.Text & "', " & _
                 "        '" & Text4.Text & "', '" & Text5.Text & "')                      " & _
                 "WHERE  date    = '" & Listdt.SelectedItem() & "' " & _
                 "  AND  agentid = '" & List1.SelectedItem() & "'  ""

    MsgBox " 1 row updated "

End Sub

Private Sub cmdExit_Click()
    cno.Close
    End
End Sub
At some point I presume you might want to re-enable the listboxes...
I haven't tested what I wrote below the dividing line. You'll need to debug it to make sure I don't have typos (I did the commenting and modifications in Word).
Let me know how this works.

You [u]have</u> to pay attention to spaces. I see that you have the habit I see here often of putting the punctuation on the wrong side of gaps in your sentences. Where you should have “Well, I don’t know. Maybe.” you have “Well ,I don’t know .Maybe.” Datatbases will not be able to interpret something like that where humans can. The single quotes that delineate strings [u]must</u> be right up against the string that they delineate. Otherwise, the space will be interpreted as part of the string, and you won’t get a match.
 
Old August 25th, 2006, 05:38 AM
Authorized User
 
Join Date: Aug 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi brian
firstof all i have to be frank with u.i just started a new job ,just outta college so pardon me for all the mistakes i do here. i copied the new code under the line --------- and its goin to this bit
With mRs
        .Open "SELECT * " & _
              "FROM tsrtime " & _
              "WHERE date = '" & Listdt.SelectedItem() & "' " & _
              " AND agentid = '" & List1.SelectedItem() & "' ", _
              mCno, adOpenStatic, adLockPessimistic

showing me an error saying compile error
method or datamember not found ..u know the [.Selecteditem]do i have to include any project /references so that it enables that method in it and iam using Visualbasic 6 SP5.
and one more thing
this bit

Private Sub cmdSave_Click()

    MCno.Execute "UPDATE tsrtime (starttime, endtime, breaklength, reasoncode, paycode) " & _
                 "VALUES ('" & Text1.Text & "', '" & Text2.Text & ', '" & Text3.Text & "', " & _
                 " '" & Text4.Text & "', '" & Text5.Text & "') " & _
                 "WHERE date = '" & Listdt.SelectedItem() & "' " & _
                 " AND agentid = '" & List1.SelectedItem() & "' ""

is all in red..looks like there sumthing wrong..
tahnks for goin thru this and spending ur precious time to help me out
thanks again.

 
Old August 25th, 2006, 05:53 AM
Authorized User
 
Join Date: Aug 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

And by the way the fucntionality is it retrieves 1 record from the database for the selected user on the selected date..iam quite confused at the eof bof bit cause this record would be the end and the begining in the recordset..?
and the listbox style property is chekbox and mutiselect property is 0 -none cause i think this is causing that error..(Am i too stupid) or reasonably stupid?? i wud like to email u the whole stuff, might make things easier for u..if u email me at [email protected]
cheers

 
Old August 28th, 2006, 09:59 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Sorry:
Code:
MCno.Execute "UPDATE tsrtime (starttime, endtime, breaklength, reasoncode, paycode)    " & _
                 "VALUES ('" & Text1.Text & "', '" & Text2.Text & "', '" & Text3.Text & "', " & _
                 "        '" & Text4.Text & "', '" & Text5.Text & "')                      " & _
                 "WHERE  date    = '" & Listdt.SelectedItem() & "' " & _
                 "  AND  agentid = '" & List1.SelectedItem() & "'  ""
                 (add the quote shown in red, line 2...)
I don't work with listboxes very often, so I always have to look up the syntax.
You don't need to add any reference for them to work. Put the cursor in the word "selecteditem" and press F1 for Help, then look around for how to access the currently selected item and substitute that code.
 
Old August 29th, 2006, 09:48 AM
Authorized User
 
Join Date: Aug 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for pointing out the error.Righto wot i have done is changed some syntaxes like instead of listdt.selecteditem ,i am using listdt.text as i can see this is holding the value during runtime.when i try to run it its throwing a Runtime error :91
Object variable or With block variable not set.
And takes me to the Highlighted area.all with blocks are fine .
please help.the other problem is Runtime error 3021 either BOF is true or the current record has been deleted.Requested operation requires a current record.
The sql gives 1 record o no record.how to tackle this error.
if no record i have msgbox saying no record ,
here is the code..

-------------------------------------------------
Option Explicit
Private mCno As ADODB.Connection
Private mRs As ADODB.Recordset
Private Sub Form_Load()

    Set mCno = New ADODB.Connection
    Set mRs = New ADODB.Recordset

    With mCno
        .ConnectionString = "driver = {sqlserver};" & _
                            "Data Source = sqldsn;" & _
                            "UserId=reddy;" & _
                            "Password=reddy04"
        .Open
    End With

    With mRs
        .Open "SELECT distinct date,agentid " & _
              "FROM tsrtime ", _
              mCno, adOpenForwardOnly

        Do Until .EOF
            Listdt.AddItem !Date
            List1.AddItem !agentid
            mRs.MoveNext
        Loop

         If .State = adStateOpen Then .Close
    End With
    Set mRs = Nothing

End Sub
Public Sub Cmdret_Click()
'Dim i As Integer
    With mCno
        If .State = adStateClosed Then
            .ConnectionString = "driver = {sqlserver};" & _
                                "Data Source = sqldsn;" & _
                                "UserId=reddy;" & _
                                "Password=reddy04"
            .Open
        End If
    End With

    With mRs
        .Open "SELECT * " & _
              "FROM tsrtime " & _
              "WHERE date = '" & Listdt.Text & "' " & _
              " AND agentid = '" & List1.Text & "' ", _
              mCno, adLockPessimistic

        If .EOF Then
            MsgBox "No Records found for user " & List1.List(List1.Text) & " On " & Listdt.List(Listdt.Text), vbExclamation Or vbOK

        Else
            Text1.Text = !starttime
            Text2.Text = !endtime
            Text3.Text = !breaklength
            Text4.Text = !reasoncode
            Text5.Text = !paycode
        End If
        .Close
    End With

    Text1.SetFocus
    List1.Enabled = False
    Listdt.Enabled = False

End Sub

Private Sub cmdSave_Click()

    mCno.Execute "UPDATE tsrtime (starttime, endtime, breaklength, reasoncode, paycode) " & _
                 "VALUES ('" & Text1.Text & "', '" & Text2.Text & "', '" & Text3.Text & "', " & _
                 " '" & Text4.Text & "', '" & Text5.Text & "') " & _
                 "WHERE date = '" & Listdt.selecteditem() & "' " & _
                 " AND agentid = '" & List1.selecteditem() & "' """

    MsgBox " 1 row updated "

End Sub

Private Sub cmdExit_Click()
    mCno.Close
    End
End Sub
---------------------------------------------






 
Old August 29th, 2006, 09:59 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

It is possible that the connection is not open. Check that.
On what line do you get the "3021: Either BOF is true or the current record has been deleted"?
 
Old August 30th, 2006, 04:32 AM
Authorized User
 
Join Date: Aug 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

yes u were right i inserted an Set mRs = New ADODB.Recordset at the cmd_ret_click and started debuggin it step by step form load is woring perfect and then when it comes into cmd_ret as there are 3 records in the database i have selected a date and id for which a record exists and then executed the app.its goin to the select statement and comes to if .eof No Record found mesage for any record,i opened my sql studio and opened up the database.
the records are stored in the following format

datetime agentid
2001-04-01 00:00:00:00.000 101
2001-03-02 00:00:00:00.000 200
2001-01-09 00:00:00:00.000 100
but in the form we have 01-04-2001,02-03-2001,09-01-2001 respectively.
may be the date isnt matching here and goes on to the EOF??is that the main problem??
Thanx






Similar Threads
Thread Thread Starter Forum Replies Last Post
plz correct my programe Basim Visual Basic 2005 Basics 1 October 30th, 2007 01:37 PM
Is it correct like this ? tunisiano C# 1 March 7th, 2007 12:22 PM
Correct code stefanocinfo BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1 2 October 4th, 2006 03:06 AM
pageload is not correct... pc_35 ASP.NET 1.0 and 1.1 Basics 5 September 26th, 2006 09:20 AM
correct syntax? crmpicco Classic ASP Basics 2 February 10th, 2005 05:32 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.