dcsimg
Results 1 to 2 of 2

Thread: Looping Through Records In Form Is Not Working MS Access

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2017
    Posts
    17

    Looping Through Records In Form Is Not Working MS Access

    To send customized SMS to multiple phone by a single click I've written the following code in MS Access VBA. A part of this code is working for the first record only.

    Method # 1:

    Code:
    Private Sub cmdSendSMS2All_Click()
        Dim rsp As Variant
        DoCmd.GoToRecord , , acFirst
    
       While Not Me.NewRecord
            rsp = client.OneToOne(UserName.Value, Password.Value, MobileNo.Value, smsText.Value, smsText, "", "")
            Me.txtDeliveryCode = rsp
            
        If Left(txtDeliveryCode, 4 = "1900") Then
            Me.txtDeliveryStatus = "Successful"
            Me.txtDeliveryDate = Date
            Me.txtDeliveryTime = Time(Now)
        Else
            Me.txtDeliveryStatus = "Failed"
            Me.txtDeliveryDate = Date
            Me.txtDeliveryTime = Time(Now)
        End If
            DoCmd.GoToRecord , , acNext
            Wend
    End Sub
    Method # 2

    Code:
    Private Sub cmdSendSMS2All_Click()
        Dim rsp As Variant
        Dim rs As Object
    Set rs = Me.RecordsetClone
            DoCmd.GoToRecord , , acFirst
            With rs
            Do While Not .EOF
            rsp = client.OneToOne(UserName.Value, Password.Value, MobileNo.Value, smsText.Value, smsText, "", "")
            Me.txtDeliveryCode = rsp
        If Left(txtDeliveryCode, 4 = "1900") Then
            Me.txtDeliveryStatus = "Successful"
            Me.txtDeliveryDate = Date
            Me.txtDeliveryTime = Time(Now)
        Else
            Me.txtDeliveryStatus = "Failed"
            Me.txtDeliveryDate = Date
            Me.txtDeliveryTime = Time(Now)
        End If
            
        .MoveNext
        Loop
        End With
        Set rs = Nothing
        
    End Sub

    It goes to the first record, send SMS to the first record and change txtDeliveryCode. It means following codes are working only.

    Code:
            DoCmd.GoToRecord , , acFirst
            rsp = client.OneToOne(UserName.Value, Password.Value, MobileNo.Value, smsText.Value, smsText, "", "")
            Me.txtDeliveryCode = rsp
    Where is the problem? How can I solve this please?
    Last edited by hafsarabic; Aug 2nd, 2018 at 02:29 PM. Reason: VB Code was marked as PHP.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    23,818

    Re: Looping Through Records In Form Is Not Working MS Access

    did you try to step through the code?

    try
    Code:
    If Left(txtDeliveryCode, 4) = "1900" Then
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width