I
iris
I have a userform1 in word that feeds from an access database.
In that form I have :
textbox1 - in which i write the string I need to search
optionbutton1 - in which I choose the column I need to search the string in
the database
comandbutton1 - to execute the search
listbox1 - in which I need the list of the results to appear
The code I wrote execute the search sucssessfully but I don't know how to
build the array for all the resualts.... I only get one resault at a time and
eventually - only the last resault appears in the list box.
I need all the resualts to appear in the listbox.
can someone please help me write the code correctly...?
This is the code I wrote:
Private Sub CommandButton1_Click()
Dim dbDatabase As Database
Dim rse As Recordset
Dim rsi As Recordset
Dim e As Integer
Dim i As Integer
Dim d As Boolean
Set dbDatabase = OpenDatabase("C:\nihul.mdb")
Set rse = dbDatabase.OpenRecordset("SELECT * FROM expressions ;",
dbOpenSnapshot)
Dim myActiveRecord As Recordset
Dim x As Long
x = 0
Dim eStr As String
Dim answer As String
d = False
i = 0
e = 0
If TextBox1.Text = "" Then
MsgBox "enter a search value", vbOKOnly + vbExclamation
d = True
Else
If OptionButton1.Value = False Then
MsgBox "choose a search category", vbOKOnly + vbExclamation
d = True
Else
If OptionButton1.Value = True Then
ListBox1.Clear
With rse
Do Until .EOF
eStr = ![heb]
If InStr(![heb], TextBox1.Text) > 0 Then
d = True
i = i + 1
TextBox2.Text = ![heb]
ListBox1.AddItem
Set dbDatabase = OpenDatabase("C:\nihul.mdb")
Set myActiveRecord = myDataBase.OpenRecordset("SELECT * FROM expressions
WHERE heb = '" & TextBox2.Text & "'", dbOpenForwardOnly)
ListBox1.ColumnCount = myActiveRecord.Fields.Count
Do While Not myActiveRecord.EOF
ListBox1.AddItem
ListBox1.ColumnCount = 7
ListBox1.BoundColumn = 7
ListBox1.ColumnWidths = "4 in;1 in;1 in;1 in;1 in;1 in;0 in;"
ListBox1.List(x, 6) = myActiveRecord.Fields("hebrt")
ListBox1.List(x, 5) = myActiveRecord.Fields("heb")
ListBox1.List(x, 4) = myActiveRecord.Fields("engRT")
ListBox1.List(x, 3) = myActiveRecord.Fields("eng")
ListBox1.List(x, 2) = myActiveRecord.Fields("employeeName")
ListBox1.List(x, 1) = myActiveRecord.Fields("lastUpdate")
ListBox1.List(x, 0) = myActiveRecord.Fields("hebshort")
x = x + x
myActiveRecord.MoveNext
Loop
myActiveRecord.Close
myDataBase.Close
Set myActiveRecord = Nothing
Set myDataBase = Nothing
answer = MsgBox("R U sure?" & TextBox1.Text & ". ,
vbQuestion)
If answer = vbNo Then
Exit Sub
Else
End If
End If
.MoveNext
e = e + 1
Loop
End With
End If
End If
End If
rse.Close
dbDatabase.Close
If d = False Then
MsgBox "no resaults are found", vbOKOnly + vbExclamation
End If
End Sub
In that form I have :
textbox1 - in which i write the string I need to search
optionbutton1 - in which I choose the column I need to search the string in
the database
comandbutton1 - to execute the search
listbox1 - in which I need the list of the results to appear
The code I wrote execute the search sucssessfully but I don't know how to
build the array for all the resualts.... I only get one resault at a time and
eventually - only the last resault appears in the list box.
I need all the resualts to appear in the listbox.
can someone please help me write the code correctly...?
This is the code I wrote:
Private Sub CommandButton1_Click()
Dim dbDatabase As Database
Dim rse As Recordset
Dim rsi As Recordset
Dim e As Integer
Dim i As Integer
Dim d As Boolean
Set dbDatabase = OpenDatabase("C:\nihul.mdb")
Set rse = dbDatabase.OpenRecordset("SELECT * FROM expressions ;",
dbOpenSnapshot)
Dim myActiveRecord As Recordset
Dim x As Long
x = 0
Dim eStr As String
Dim answer As String
d = False
i = 0
e = 0
If TextBox1.Text = "" Then
MsgBox "enter a search value", vbOKOnly + vbExclamation
d = True
Else
If OptionButton1.Value = False Then
MsgBox "choose a search category", vbOKOnly + vbExclamation
d = True
Else
If OptionButton1.Value = True Then
ListBox1.Clear
With rse
Do Until .EOF
eStr = ![heb]
If InStr(![heb], TextBox1.Text) > 0 Then
d = True
i = i + 1
TextBox2.Text = ![heb]
ListBox1.AddItem
Set dbDatabase = OpenDatabase("C:\nihul.mdb")
Set myActiveRecord = myDataBase.OpenRecordset("SELECT * FROM expressions
WHERE heb = '" & TextBox2.Text & "'", dbOpenForwardOnly)
ListBox1.ColumnCount = myActiveRecord.Fields.Count
Do While Not myActiveRecord.EOF
ListBox1.AddItem
ListBox1.ColumnCount = 7
ListBox1.BoundColumn = 7
ListBox1.ColumnWidths = "4 in;1 in;1 in;1 in;1 in;1 in;0 in;"
ListBox1.List(x, 6) = myActiveRecord.Fields("hebrt")
ListBox1.List(x, 5) = myActiveRecord.Fields("heb")
ListBox1.List(x, 4) = myActiveRecord.Fields("engRT")
ListBox1.List(x, 3) = myActiveRecord.Fields("eng")
ListBox1.List(x, 2) = myActiveRecord.Fields("employeeName")
ListBox1.List(x, 1) = myActiveRecord.Fields("lastUpdate")
ListBox1.List(x, 0) = myActiveRecord.Fields("hebshort")
x = x + x
myActiveRecord.MoveNext
Loop
myActiveRecord.Close
myDataBase.Close
Set myActiveRecord = Nothing
Set myDataBase = Nothing
answer = MsgBox("R U sure?" & TextBox1.Text & ". ,
vbQuestion)
If answer = vbNo Then
Exit Sub
Else
End If
End If
.MoveNext
e = e + 1
Loop
End With
End If
End If
End If
rse.Close
dbDatabase.Close
If d = False Then
MsgBox "no resaults are found", vbOKOnly + vbExclamation
End If
End Sub