Help with looping

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Clant1
    New Member
    • Feb 2008
    • 13

    Help with looping

    im trying to get a form to let me change the record im looking at though a text box. This hasnt worked any of the ways ive tried yet but im not the best at vb. my idea was to make a button that returns you to the first value then sends you to the next record X ammount of times. So far i have
    [code=vb]
    Private Sub Command8_Click( )

    Dim rec As Integer
    Dim count As Integer
    count = 0
    rec = Text12
    DoCmd.GoToRecor d , , acFirst

    Exit_Command9_C lick:
    Exit Sub

    Err_Command9_Cl ick:
    MsgBox Err.Description
    Resume Exit_Command9_C lick

    Do Until count = rec
    count = count + 1
    DoCmd.GoToRecor d , , acNext
    Loop
    End Sub[/code]
    Last edited by debasisdas; Feb 21 '08, 09:50 AM. Reason: added code=vb tags
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    are you retriving data from database ?

    Comment

    • Clant1
      New Member
      • Feb 2008
      • 13

      #3
      Originally posted by debasisdas
      are you retriving data from database ?
      yes. all im trying to do is make it change the record im looking at. so i thought if it looped the look at next record command it should do that

      Comment

      • jamesd0142
        Contributor
        • Sep 2007
        • 471

        #4
        If your using a database, and you load a single record into a textbox, when you change the value in the textbox you will need to run some script in sql to update the record...

        Such as (example only)

        before this works you should store the id of the record in a variable on loading into the textbox.

        [code=text]
        update tablename
        set fieldname = 'textbox.text' where id = 'IDvariable'
        [/code]

        Comment

        • jamesd0142
          Contributor
          • Sep 2007
          • 471

          #5
          Here's an example in vb.net

          [code=vbnet]
          Imports System.Data.Ole Db

          public Class Form1

          '---Database Declarations
          Dim Cmd As OleDb.OleDbComm and
          Dim Con As OleDb.OleDbConn ection
          Dim Sql As String = Nothing
          Dim Reader As OleDb.OleDbData Reader
          Dim ComboRow As Integer = -1
          Dim Columns As Integer = 0
          Dim Category As String = Nothing
          Dim da As System.Data.Ole Db.OleDbDataAda pter
          Dim oDS2 As New System.Data.Dat aSet
          Dim ID As String = 1
          '---Database Declarations End



          Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click
          txtDisplay.Clea r()
          oDS2.Clear()

          Dim value As String = Nothing

          Sql = "select * from Change where [IDVal] = " & "'" & ID & "'"
          Con = New OleDbConnection ("Provider=Micr osoft.Jet.OLEDB .4.0;Data Source=C:\Docum ents and Settings\james. durbin\My Documents\remin der.mdb")

          Con.Close()
          Cmd = New OleDb.OleDbComm and(Sql, Con)
          Try
          Con.Open()
          da = New OleDbDataAdapte r(Sql, Con)
          da.Fill(oDS2, "change")
          If oDS2.Tables("ch ange").Rows.Cou nt = 1 Then
          txtDisplay.Text = oDS2.Tables("ch ange").Rows(0)( "Value")
          End If
          Catch

          End Try
          End Sub

          Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button2.Click
          Try
          'Dim StrConn As String = "Provider=Micro soft.Jet.OLEDB. 4.0;Data Source=c:\Users \Users.mdb"
          Dim oConn As New System.Data.Ole Db.OleDbConnect ion("Provider=M icrosoft.Jet.OL EDB.4.0;Data Source=C:\Docum ents and Settings\james. durbin\My Documents\remin der.mdb")
          oConn.Close()
          Dim con As New System.Data.Ole Db.OleDbCommand
          con.Connection = oConn
          con.CommandType = CommandType.Tex t
          con.CommandText = "update change set [Value] = " & "'" & txtDisplay.Text & "'" & " where [IDVal] = " & "'" & ID & "'"
          oConn.Open()
          'display no of rows affected
          con.ExecuteNonQ uery()
          Catch
          End Try
          End Sub

          Private Sub TextBox2_TextCh anged(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles txtID.TextChang ed
          ID = txtID.Text
          End Sub
          End Class

          [/code]

          my form had 2 textbox's
          txtId
          txtDisplay

          2 buttons
          button1
          button2

          im using an access Database

          James

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Originally posted by Clant1
            im trying to get a form to let me change ...
            If you carefully read through the code you posted in message #1, there is no way it can possibly reach the Do loop. Consider line 10 and (assuming there's an On Error Goto that we can't see) line 14. Both of which return control to somewhere else.

            By the way, I recommend changing the variable name count. Notice it's shown in a different colour to rec, when formatted here on TheScripts? I think that's because it's a reserved word in VB.
            Last edited by Killer42; Feb 21 '08, 09:50 PM.

            Comment

            • Clant1
              New Member
              • Feb 2008
              • 13

              #7
              Originally posted by Killer42
              If you carefully read through the code you posted in message #1, there is no way it can possibly reach the Do loop. Consider line 10 and (assuming there's an On Error Goto that we can't see) line 14. Both of which return control to somewhere else.

              By the way, I recommend changing the variable name count. Notice it's shown in a different colour to rec, when formatted here on TheScripts? I think that's because it's a reserved word in VB.
              tried what you said first and ended up with

              [CODE=vb]Private Sub Command8_Click( )

              Dim rec As Integer
              Dim cnt As Integer
              Dim i As Integer
              cnt = 0
              rec = Text12
              DoCmd.GoToRecor d , , acFirst

              Do Until cnt = rec
              cnt = cnt + 1
              DoCmd.GoToRecor d , , acNext

              Loop
              End Sub[/CODE]

              which worked. out of interest i did try the others and they worked as well thanks alot guys
              Last edited by Killer42; Feb 22 '08, 12:39 PM. Reason: Added CODE=vb tag

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                Glad we could help. :)

                Comment

                Working...