Results 1 to 10 of 10

Loop to execute SAVE code as defined number of times

Threaded View

  1. #1
    Forum Contributor
    Join Date
    02-20-2009
    Location
    bristol
    MS-Off Ver
    Excel 2003
    Posts
    883

    Loop to execute SAVE code as defined number of times

    Hi All

    When a userform is loaded then initially a textbox appears asking total number of documents. And then when the user types the number and press tab then the next textboxes appear:
    1. Policy no
    2. Batch no
    3. Document type
    4. Document Prov

    This Quantity textbox drives the number of iterations per input on the form after SAVE button pressed although policy number and batch number textboxes can be left populated.At each SAVE, the values of document type and Document Prov are loaded into the memory of userform FrmReturn.

    The following code will keep on running as I haven't defined how many times it has to run. If there are 5 documents then the code will run 5 times and then after last iteration FrmReturn will be displayed. Can anyone please help me in this?
    Private Sub CommandButton1_Click()
    
            Dim cn As ADODB.Connection
            Dim rs As ADODB.Recordset
            Dim r As Long
            
            Set cn = New ADODB.Connection
            cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
            "Data Source=J:\ASystem.mdb;"
                 
            If ComboBox2.Value = "Original" Then
                strsql = "Select Original from tblDocType where DocumentType='" & ComboBox1.Value & "' and PolicyNoRangeBeginsWith='" & Left(TextBox1.Value, 1) & "'"
                ElseIf ComboBox2.Value = "Certified Copy" Then
                strsql = "Select CertifiedCopy from tblDocType where DocumentType='" & ComboBox1.Value & "' and PolicyNoRangeBeginsWith='" & Left(TextBox1.Value, 1) & "'"
                Else
                strsql = "Select PhotoCopy from tblDocType where DocumentType='" & ComboBox1.Value & "' and PolicyNoRangeBeginsWith='" & Left(TextBox1.Value, 1) & "'"
            End If
         
            Set rs = CreateObject("ADODB.Recordset")
        
            rs.Open strsql, cn
            
            If rs.EOF = True Then
                 Exit Sub
                Else
                MsgBox rs.Fields(0).Value
            End If
            
          With FrmReturn.Controls("ListBox1")
        .AddItem ComboBox1.Value
        .List(.ListCount - 1, 1) = ComboBox2.Value
    End With
            FrmReturn.Controls("textbox23").Value = TextBox1.Value
            FrmReturn.Controls("textbox24").Value = TextBox3.Value
          
            ComboBox1.Value = ""
            ComboBox2.Value = ""
              
            
            rs.Close
           
            cn.Close
            Set rs = Nothing
            Set cn = Nothing
    End Sub
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. I need a macro that will let me loop a section of the vba code x number of times
    By tuckejam in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-28-2013, 02:44 PM
  2. [SOLVED] VBA loop to copy a worksheet a number of times, assign name each time based on loop number
    By TBG in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-29-2012, 10:54 PM
  3. Copy and Paste a defined number of times...
    By ccxc007 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-15-2012, 02:10 AM
  4. Replies: 1
    Last Post: 07-15-2012, 06:31 AM
  5. Code executes 8 times without loop statements. Why?
    By contaminated in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-08-2010, 05:02 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1