添加新紀錄
rst.AddNew
rst.Fields(0)=trim(text1.text) ’把文本框內容賦值給字段
rst.Fields(1)=trim(text2.text)
rst.update ’結果集更新
ok=true
rst.close ’關閉連接
msgbox"添加成功" ’添加成功
如何判斷輸入的日期格式是否正確
If Not IsDate(txtDate.Text) Then
Msgobx"請輸入yyyy-mm-dd格式的日期",0+1,"溫馨提示"
txtDate.Text = ""
txtDate.SetFocus
End If
如何判斷出生日期是否早於入學日期
Private Sub txtInDate_Change()
If IsDate(Trim(txtInDate.Text)) And IsDate(Trim(txtBirDate.Text)) Then
if CDate(Trim(txtInDate.Text)) < CDate(Trim(txtBirDate.Text)) Then
MsgBox"入校日期要晚於出生日期",0+1,"溫馨提示"
txtInDate.Text = ""
txtBirDate.Text =""
txtInDate.SetFocus
txtBirDate.SetFocus
Exit Sub
End If
End If
End Sub
如何判斷與數據庫數據是否重複
'此處判斷學號
If Not rst.EOF Then
MsgBox"已存在此學號,請重新輸入",0+1,"溫馨提示"
txtid.Text = ""
txtid.SetFocus
End If
如何判斷輸入內容是否是數字
’此處以判斷輸入的年齡爲例
If Not IsNumeric(txtAge.Text) then
Msgbox"您輸入的年齡不是數字,請重新輸入!",0+1,"溫馨提示"
txtAge.Text=""
txtAge.SetFocus
End If
如何讓文本框只能輸入數字
‘此處以判斷輸入的學號爲例
Private Sub txtid_KeyPress(KeyAscii As Integer)
If KeyAscii <> 8 And (KeyAscii < 48 Or KeyAscii > 57) Then
KeyAscii = 0
MsgBox"您輸入的不是數字,請重新輸入",0+1,"溫馨提示"
txtid.Text=""
txtid.SetFocus
End If
End Sub
'在Ascii碼錶中,Ascii值48-57爲阿拉伯漢字0-9,如果用戶輸入的內容超過了這幾個值,則提示錯誤信息
’在Ascii碼錶中,碼值爲8的表示退格鍵
如何限制文本框輸入內容的字符長度
'此處以判斷輸入的用戶名爲例
If Len(txtUser.Text) > 5 Then
MsgBox"字符長度過長!",0+1,"溫馨提示"
txtUser.Text=""
txtUser.SetFocus
Exit Sub
End Sub
如何使文本框只能輸入漢字
'此處以判斷輸入的姓名爲例
Private Sub txtUser_Keypress(Keyascii As Integer)
If Keyascii >= -20319 And KeyAscii <= -3652 Or keyascii =8 then
Else
keyascii = 0
MsgBox"您輸入的不是漢字,請重新輸入!",0+1,"溫馨提示"
End If
End Sub
說明:在Key ASCII中,-20319到-3652爲漢字輸入範圍,碼值8爲backspace爲退格鍵
'如果用戶執行的是漢字鍵盤,且退格鍵,則輸入有效,否則,輸入無效
如何將從數據庫檢索到的數據顯示在文本框中
說明:使用窗體加載事件,連接數據庫,將數據表中的字段賦值給文本框
Private Sub Form_Load()
Dim txtSQL, msgText As String
txtSQL = "select * from student_Info" '連接數據庫,執行SQL語句
Set rst = ExecuteSQL(txtSQL, msgText)
rst.MoveFirst ’將指針放在結果集第一個記錄
txtuser.Text = rst.Fields(0) '數據表的第一個字段賦值txtuser文本框
txtid.Text = rst.Fields(1)
txtAge.Text = rst.Fields(2)
txtAddr.Text = rst.Fields(3)
End Sub
如何文本框中的數據處於不能編輯狀態
Private Sub Form_Load()
txtUser.Enabled = False
txtid.Enabled =False
txtAge.Enabled = False
txtaddr.Enabled = False
End Sub
如何讓下拉框中爲數據表制定的字段
說明:此處爲加載班級表中,班級編號字段,使用Do While...Loop循環語句
txtSQL="select * from 班級表 "
Set rst = ExecuteSQL(txtSQL, msgText)
Do While Not rst.EOF
Combo1.AddItem rst("班級編號_Type")
rst.MoveNext
Loop
rst.Close
如何限制密碼輸入次數
方法一:當用戶密碼輸入錯誤超過3次,則退出窗體
Private Sub Command1_Click()
Dim i As Integer
i = i + 1
If i = 3 Then
Me.Hide
End If
方法二:每輸入錯誤一次就提示
Private Sub Command1_Click()
Dim i As Integer
i = i + 1
if i = 1 Then
MsgBox "很抱歉,您的密碼輸入錯誤,還有2次機會哦~",0+1,"溫馨提示"
txtpassword.Text = ""
Exit Sub
End If
If i = 2 Then
MsgBox "很抱歉,您的密碼輸入錯誤,還有1次機會哦~",0+1,"溫馨提示"
txtpassword.Text = ""
Exit Sub
End If
If i = 3 Then
MsgBox "很抱歉,您的密碼輸入錯誤,還有0次機會哦~",0+1,"溫馨提示"
txtpassword.Text = ""
Exit Sub
End If
End Sub
重在多練習,多用於實際操作中,效果更佳!