我对Visual Basic 6.0还是很陌生,但对它的了解却很少。 我的项目是关于机场登机牌发生器。
在我的第一个表格中,我有一个文本框,供用户输入PNR。在此,程序必须搜索已存储在与ADODB连接的MS访问数据库中的PNR。如果用户输入的PNR正确,则必须转到下一张显示旅客和航班的完整详细信息的表格,否则应弹出一个味精。框中显示“无效的PNR”。
这是我在该程序中使用的编码:
Dim conn As ADODB.Connection
Dim RS As New ADODB.Recordset
Private Sub Form_Load()
Set conn = New ADODB.Connection
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\16hu3a120(part2)\BOARDING PASS3.mdb;Persist Security Info=False"
conn.Open
conn.CursorLocation = adUseClient
RS.Open "select * from PID", conn, adOpenDynamic, adLockOptimistic
End Sub
Private Sub cmdcancel_Click(Index As Integer)
MsgBox "DO YOU WANT TO CANCEL THE PROCESS?", vbYesNoCancel
End
End Sub
Private Sub cmdconfirm_Click(Index As Integer)
Dim pnr As String
pnr = txtinput.Text
Do
If (pnr = RS.Fields(0)) Then
MsgBox "SUCCESSFUL"
Form1.Show
ElseIf (pnr <> RS.Fields(0) & RS.EOF = True) Then
MsgBox "Invalid PNR", vbRetryCancel
RS.MoveFirst
End
Exit Do
End If
RS.MoveNext
Loop While RS.EOF = False
If pnr = "" Then
MsgBox " FIELDS CANNOT BE LEFT EMPTY", vbAbortRetryIgnore
End If
End
Exit Sub
End Sub
预先感谢
答案 0 :(得分:0)
您的代码中有几个错误。
在“取消”按钮事件代码中,您需要处理消息框中的返回值并采取相应措施。
Private Sub cmdcancel_Click(sender As Object, e As EventArgs) Handles cmdCancel.Click
'
If MsgBox("DO YOU WANT TO CANCEL THE PROCESS?", vbYesNoCancel) <> vbNo Then
'Hide the form and return to main window
Me.Hide
End If
'
End Sub
在“确认”按钮事件代码中,您需要在处理输入之前(而不是在处理结束时)测试输入!
还需要简化操作顺序,以便以尽可能少的步骤/陈述来完成目标。
Private Sub cmdconfirm_Click(sender As Object, e As EventArgs) Handles cmdConfirm.Click
'
Dim matched as boolean
Dim pnr As String
pnr = txtinput.Text
'
If pnr = "" Then
MsgBox("FIELDS CANNOT BE LEFT EMPTY", vbOkay)
Else
matched = false
Do
If (pnr = RS.Fields(0)) Then
matched = True
exit do
End If
RS.MoveNext
Loop While RS.EOF = False
'
if matched Then
MsgBox("SUCCESSFUL")
Form1.Show
Else
MsgBox("Invalid PNR", vbOkay)
'return to main form
Me.Hide
End If
End If
'
End Sub
最后,尽管我没有将其包含在代码中,但请务必丢弃创建和使用的变量,以避免内存泄漏!