Home > Computer Tutorials > Computer Knowledge > How to use VB to loop through database strings and add them to drop-down list boxes

How to use VB to loop through database strings and add them to drop-down list boxes

WBOY
Release: 2024-01-25 12:06:21
forward
818 people have browsed it

How to use VB to loop through database strings and add them to drop-down list boxes

How does vb loop to query a string in a database and add it to the drop-down list box

Whether you use

Data1.Recordset.FindFirst method

Still use

Data1.Recordset.FindNext method

To search for records, you must use

Data1.Recordset.NoMatch

To determine whether matching records are found, if

Data1.Recordset.NoMatch returns True, which means it is not found. If it returns False, it means it is found.

but not directly (preferably not directly) like you use:

Data1.Recordset.FindNext "Field 2 Like" & "'*" & Text8.Text & "*'""

Combo1.AddItem Data1.Recordset.Fields(1)

But instead:

Data1.Recordset.FindNext "Field 2 Like" & "'*" & Text8.Text & "*'""

if Data1.Recordset.NoMatch Then

'Write here the corresponding code to terminate the search

else

Combo1.AddItem Data1.Recordset.Fields(1)

.........

End If

What I said above is best not to use it directly like you. It means that it can still be used directly in some places. That is, are you sure that the record you are looking for must exist?

Simple vb string processing

'Add a TextBox (named Text1, MultiLine attribute is set to True, used for input), a CommandButton (named Command1, used to start analysis), and a ListBox (named List1, used to prove that the array analysis is correct) .

'Add the following code.

Option Explicit

Private Sub Command1_Click()

Dim Num() As Long, i As Long, j As Long

List1.Clear

If StrToNum(Text1.Text, Num) Then

ReDim t(0 To UBound(Num, 2)) As String

For i = 0 To UBound(Num, 1)

For j = 0 To UBound(Num, 2)

t(j) = CStr(Num(i, j))

Next

List1.AddItem Join(t)

Next

Else

MsgBox "Wrong Input!"

End If

End Sub

Private Function StrToNum(S As String, N() As Long) As Boolean

Dim A() As String, B() As String, C As Long, D As String, E As Boolean, F As Long

Dim i As Long, j As Long

If Len(S) = 0 Then Exit Function

A = Split(S, vbCrLf)

For i = 0 To UBound(A)

Erase B

C = 0

E = False

For j = 1 To Len(A(i))

D = Mid(A(i), j, 1)

Select Case Asc(D)

Case 48 To 57

If Not E Then

ReDim Preserve B(0 To C)

C = C 1

E = True

End If

B(C - 1) = B(C - 1) & D

Case 32

E = False

Case Else

Exit Function

End Select

Next

If C = 0 Then Exit Function

If i = 0 Then

ReDim N(0 To UBound(A), 0 To C - 1)

F = C

Else

If F C Then Exit Function

End If

For j = 0 To C - 1

N(i, j) = Val(B(j))

Next

Next

StrToNum = True

End Function

The above is the detailed content of How to use VB to loop through database strings and add them to drop-down list boxes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:docexcel.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template