VBA code for Next button to move to each record in Textbox1

chazrab

Well-known Member
Joined
Oct 21, 2006
Messages
884
Office Version
  1. 365
Platform
  1. Windows
This code very efficiently displays all records from a MaketTable query and displays them in a large texbox separated with vbNewline & vbnewline
for easier reading:
Code:
Private Sub cmdTestCode_Click()
    Dim x As String
    Dim rs As Recordset
    Dim db As Database
    Dim strSQL As String
    Dim srchval As String
    x = Me.txtSearchCriteria.Value
    srchval = Me.txtSearchCriteria.Value
    With DoCmd
        .SetWarnings False
        .OpenQuery "qmakSearch" 'qmakSearch is a make table query that must exist on the Db
        .SetWarnings True
    End With
    If DCount("*", "tblSearchResults") > 0 Then 'tblSearchResults is the MakeTable created table from qmkSearch query
       Me.Controls("txtMatchedVerses").Value = "" 'Clear - Note txtMatchedVerses is the name of the text box thta must exist on the form to receive the results of the SQL query.
        strSQL = "SELECT * FROM tblSearchResults;"
        Set db = CurrentDb: Set rs = db.OpenRecordset(strSQL)
        rs.MoveFirst
        Do Until rs.EOF
            Me.Controls("txtMatchedVerses").Value = IIf(Len(Me.Controls("txtMatchedVerses").Value) = 0, rs.Fields(0).Value, Me.Controls("txtMatchedVerses").Value & vbNewLine & vbNewLine & rs.Fields(0).Value)
            rs.MoveNext
        Loop
           Set rs = Nothing: Set db = Nothing
       MsgBox "There were " & DCount("*", "tblSearchResults") & " records found.", vbInformation
     Else
        MsgBox "There were no records found.", vbExclamation
    End If
    Me.Totrows = DCount("*", "tblSearchResults")
End Sub
An image of this does is below. How would a button be coded to tell Access that every time
the Next button is clicked it moved to the next record - in Textbox1. It would have to read
Len of the text, add two spaces for vbnewline & vbnewline. If correct the cursor should be on
the first line of the next record(paragraph)(verse)

Have never worked with the Len statement before, and still not sure if this is the bes way to
achieve this.

Any guidance and help would be greatly appreciated. Image below are records which each Next button click
would advance to
 

Attachments

  • ACTUAL RECORDS JUST DISPLAYED IN A LARGE TEXXTBOX SEPARATED  BY 2 VBNEWLINE STMTS.png
    ACTUAL RECORDS JUST DISPLAYED IN A LARGE TEXXTBOX SEPARATED BY 2 VBNEWLINE STMTS.png
    137.6 KB · Views: 10

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
I would probably look for your newline newline.
Use Instr() for that and SelStart.
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top