vba考点例题解析

时间:2021-03-06 01:06:16

斐波拉契数列

  • 1 1 2 3 5 7 9...
  • 求前20项的和
Option Compare Database
Private Sub btnP_Click()
    Dim i As Integer
    Dim s As Integer
    '*****Add1*****
    Dim f(19) '定义数组,长度为19
    s = 2
    '*****Add1*****    
    f(1) = 1 : f(2) = 1    
    For i = 3 To 19
        '*****Add2*****
        f(i) = f(i - 1) + f(i - 2)
       s = s + f(i)
        '*****Add2*****
    Next i
    
    '数据输出到文本框内
    '*****Add3*****
	'me可能代表当前窗体,tData控件名称
    Me.tData.Value = s 	
    '*****Add3*****
    
    '以下是文件操作
    If Dir(CurrentProject.Path & "\out.dat", vbDirection) <> vbNullString Then
       Kill CurrentProject.Path & "\out.dat"
    End If
    Open CurrentProject.Path & "\out.dat" For Output As #1
        Print #1, Me!tData
    Close #1
End Sub

设置控件颜色

Option Compare Database

Private Sub Form_Load()
    '设置Label1标签为红色文本显示
    '*****Add1*****"
    Me.Label1.ForeColor = vbRed
	Me.Label1.ForeColor = rgb(255,0,0)	
    '*****Add1*****"
End Sub

'预览输出
Private Sub Cmd1_Click()
    '*****Add2*****"
    mdPnt acViewPreview
    '*****Add2*****"
End Sub

'打印输出
Private Sub Cmd2_Click()
    mdPnt acViewNormal
End Sub

'输出过程
Private Sub mdPnt(flag As Integer)
    '按照参数条件输出
    'doCmd一个类或一个对象
    DoCmd.OpenReport "rEmp", flag
End Sub

刷新和关闭窗体

Option Compare Database
Private Sub bt1_Click()
    '动态设置窗体记录源属性
    '*****Add1*****
   '可以使用 RecordSource 属性指定窗体的数据源
    Form.RecordSource = "qEmp" 
    '*****Add1*****
    '刷新窗体
    Me.Requery
End Sub

Private Sub bt2_Click()
    '关闭当前窗体
    '*****Add2*****
    DoCmd.Close
    '*****Add2*****
End Sub
Private Sub Form_Current()

End Sub

提示框

Option Compare Database
Private Sub bt1_Click()
    '消息框提示报表输出(外观样式见题干图例)
    '*****Add*****
    '提示框有“是”和“否”两个选项
    MsgBox "报表预览", vbQuestion + vbYesNo, "确认"
    '*****Add*****
	'预览方式输出报表rEmp
	DoCmd.OpenReport "rEmp", acViewPreview
    
End Sub
Option Compare Database
Private Sub bt1_Click()
    '消息框提示报表输出(外观样式见题干图例)
    '*****Add*****
    Dim a
    '提示框内容:"报表预览"
    '提示框样式:有确定and否
    '提示框标题:"确定"
	'注意:定义a
    a = MsgBox("报表预览", vbQuestion + vbYesNo, "确认")
    If a = vbYes Then
    '*****Add*****
        '预览方式输出报表rEmp
        DoCmd.OpenReport "rEmp", acViewPreview
    End If
End Sub
Option Compare Database

Private Sub CmdEdit_Click()
      用户名_1.Enabled = True
      Me!Lremark.Visible = True
      Me!口令_1.Visible = True
      Me!备注_1.Visible = True
      Me!tEnter.Visible = True
      CmdSave.Enabled = True
End Sub

Private Sub cmdquit_Click()
DoCmd.Close
End Sub
Private Sub CmdSave_Click()
  If Me!口令_1 = Me!tEnter Then
    DoCmd.RunSQL ("update tUser " & "set 用户名='" & Me!用户名_1 & "'" & "where 用户名='" & Me!用户名_1 & "'")
    DoCmd.RunSQL ("update tUser " & "set 口令='" & Me!口令_1 & "'" & "where 用户名='" & Me!用户名_1 & "'")
    DoCmd.RunSQL ("update tUser " & "set 备注='" & Me!备注_1 & "'" & "where 用户名='" & Me!用户名_1 & "'")
    Forms!fEdit.Refresh
    DoCmd.GoToControl "cmdedit"
    CmdSave.Enabled = False
    Me!用户名_1 = Me!用户名
    Me!口令_1 = Me!口令
    Me!备注_1 = Me!备注
    Me!tEnter = " "
    Me!用户名_1.Enabled = False
    Me!口令_1.Visible = False
    Me!备注_1.Visible = False
    Me!tEnter.Visible = False
    Me!Lremark.Visible = False
  Else
  '*************************Add*************************'
   MsgBox "请重新输入口令!", vbOkquit
   MsgBox "请重新输入口令!", vbOkOnly
  '*************************Add*************************'
  End If
End Sub

Private Sub Form_Load()
    Me!用户名_1 = Me!用户名
    Me!口令_1 = Me!口令
    Me!备注_1 = Me!备注
End Sub

Select语句

Option Compare Database

Private Sub btnP_Click()
Dim k As String

'*****Add1*****
'输入提示框
k = inputbox("请输入大于0的整数")
msgbox "你好"
'*****Add1*****
If k = "" Then Exit Sub
Select Case Val(k)
    Case Is >= 3
        DoCmd.RunMacro "mEmp"
    Case 2
        '*****Add2*****
        DoCmd.OpenReport "rEmp", acViewPreview
        '*****Add2*****
    Case 1
        DoCmd.Close
End Select
End Sub
Option Compare Database

Private Sub btnP_Click()
	Dim k As String
	k = InputBox("请输入大于0的整数值")
	If k = "" Then Exit Sub
	Select Case Val(k)
		'*****Add1*****
		Case Is >= 3
		'*****Add1*****
			'调用宏对象“mEmp”
			DoCmd.RunMacro "mEmp"
		Case 2
			'*****Add2*****
			'预览输出报表对象“rEmp”
			DoCmd.OpenReport "rEmp", acViewPreview
			'*****Add2*****
		Case 1
			DoCmd.Close
	End Select
End Sub

输入文本修改控件名称

Private Sub bt1_Click()
    '*****Add*****
    'bTitle的名称=tText输入的值
    Me.bTitle.Caption = Me.tText.Value
	Me.bTitle.Caption = tText.Value
	
	bTitle.Caption = Me.tText.Value
	bTitle.Caption = tText.Value
    '*****Add*****
End Sub

素数的个数

Private Function sushu(v As Integer) As Boolean
    Dim i As Integer
    For i = 2 To v - 1
        If v Mod i = 0 Then
            sushu = False
            Exit Function
        End If
    Next
    sushu = True
End Function

Private Sub btnP_Click()
    Dim n As Integer
    Dim mn As Integer
    Dim i As Integer
   '***** Add2 *****
   n = 0
   mn = 0
    For i = 2 To 10000
        If sushu(i) And i > mn Then
            n = n + 1
            mn = i
        End If
    Next i
    '***** Add2 *****
    tData.SetFocus
    tData.Text = CStr(n) & "," & CStr(mn) ' CStr输出
End Sub

Private Sub btnQ_Click()
    '代码调用宏对象"mEmp"
    '***** Add3 *****
   DoCmd.RunMacro "mEmp"
    '***** Add3 *****
End Sub

Private Sub Form_Current()
'设置窗体标题为标签"bTitle"的标题内容
'***** Add1 *****
Me.Caption = bTitle.Caption
'***** Add1 *****
End Sub

显示子窗体的内容

vba考点例题解析

Private Sub cItem_AfterUpdate()
'************************* Add1 *************************
'Ldetail的名称=cItem的值连接字符串(内容)
   Ldetail.Caption = cItem.Value & "内容"
   me.Ldetail.Caption = cItem.Value & "内容"
'************************* Add1 *************************
Me.Refresh
End Sub

Private Sub CmdClear_Click()
  [TxtDetail] = " "
  [cItem] = " "
End Sub

Private Sub CmdList_Click()
'************************* Add2 *************************
'子窗体(fDetail)的数据来自表(tStud)中的数据
 '注意:有分号
  fDetail.Form.RecordSource = "select * from tStud;"
'************************* Add2 *************************
End Sub

Private Sub CmdRefer_Click()
  If Trim(Me![TxtDetail]) <> "" And Trim(Me![cItem]) <> "" Then
     fDetail.Form.RecordSource = "select * from tStud where " & LTrim(Me.[cItem]) & " like '" & Me![TxtDetail] & "*'"
  Else
'************************* Add3 *************************
 '提示框 提示内容 只有确定按钮 标题框标题为“注意”
      MsgBox "查询项目和查询内容不能为空!!!", vbOKOnly, "注意"
'************************* Add3 *************************
  End If
End Sub

设置窗体背景

Private Sub Form_Load()
   '设置窗体背景图片
   '*****Add*****
   Picture = CurrentProject.Path & "\test.jpg"
   Picture = CurrentProject.Path + "\test.jpg"
   '*****Add*****
End Sub

删除空格

Private Sub bt1_Click()
'*****Add1*****
'Trim删除字符串前面和后面的空格
'注意:连接字符用 &
bTitle.Caption = Trim(tText.Value) & "等级考试测试"
me.bTitle.Caption = Trim(tText.Value) & "等级考试测试"
'*****Add1*****
End Sub

判断表中是否有相同的数据

Private Sub bt1_Click()
    Dim ADOcn As New ADODB.Connection
    Dim ADOrs As New ADODB.Recordset
    Dim strDB As String
    
    '建立连接
    Set ADOcn = CurrentProject.Connection

    ADOrs.ActiveConnection = ADOcn
    ADOrs.Open "Select 编号 From tEmp Where 编号='" + tNo + "'", , adOpenForwardOnly, adLockReadOnly
    '*****Add1*****
    '判断表中是否有相同的数据
    If Not ADOrs.EOF Then
    '*****Add1*****
        MsgBox "该编号已存在,不能追加!"
    Else
        strSQL = "Insert Into tEmp (编号,姓名,性别,年龄)"
        strSQL = strSQL + "Values('" + tNo + "','" + tName + "','" + tSex + "'," + tAge + ")"
        ADOcn.Execute strSQL
        MsgBox "添加成功,请继续!"
    End If

    ADOrs.Close
    ADOcn.Close
    Set ADOrs = Nothing
    Set ADOcn = Nothing

End Sub

焦点

Private Sub cmdEnter_Click()
    Dim name As String, pass As String
    name = Nz(Me!tUser)
    pass = Nz(Me!tPass)
' *********************** Add1 *************************************
  '标签.name:指定一个控件或对象的名称或与 Font 对象相关的字体的名称
  'tPass的值="1234"
  '注意:因为已经定义了name和tPass要直接使用
   If name = "cueb" And tPass = "1234" Then
' *********************** Add1 *************************************
        MsgBox "密码输入正确,欢迎进入系统!", vbOKOnly + vbCritical, "欢迎"  '显示消息框
        DoCmd.Close
    Else
        MsgBox "密码错误!", vbOKOnly   '显示消息框
        Me!tUser = ""   '使文本框清空
        Me!tPass = ""
' *********************** Add2 *************************************
        '将光标移到tUser处
        tUser.SetFocus
' *********************** Add2 *************************************
    End If
End Sub

禁用按钮

Private Sub CmdSave_Click()
  If Me!tPass = Me!tEnter Then
    DoCmd.RunSQL ("update tUser " & "set 用户名='" & Me!tUser_1 & "'" & "where 用户名='" & Me!tUser_1 & "'")
    DoCmd.RunSQL ("update tUser " & "set 口令='" & Me!tPass & "'" & "where 用户名='" & Me!tUser_1 & "'")
    DoCmd.RunSQL ("update tUser " & "set 备注='" & Me!tRemark_1 & "'" & "where 用户名='" & Me!tUser_1 & "'")
    Forms!fEdit.Refresh
    DoCmd.GoToControl "cmdedit"
' *********************** Add1 *************************************
    '将禁用按钮关闭
    CmdSave.Enabled = True
' *********************** Add1 *************************************
    Me!tUser_1 = Me!用户名
    Me!tPass = Me!口令
    Me!tRemark_1 = Me!备注
    Me!tEnter = " "
    Me!tPass.Visible = False
    Me!tRemark_1.Visible = False
    Me!tEnter.Visible = False
    Me!Lremark.Visible = False
' *********************** Add2 *************************************
        '禁止使用按钮
        '按钮名称.Enabled  = False
        CmdSave.Enabled = False
' *********************** Add2 *************************************
  Else
' *********************** Add3 *************************************
        'vbOKQuit根本没有这条语法
        MsgBox "请重新输入口令!", vbOKQuit, "退出"         
' *********************** Add3 *************************************
  End If
End Sub

设置标题

Private Sub Form_Load()
    '设置窗体标题为"****年度报表输出"
'***** Add1 *****
Me.Caption = Year(Date) & "年度报表输出"
Form.Caption = Year(Date) & "年度报表输出"
Form.bTitle.Caption = Year(Date) & "年度报表输出"
'***** Add1 *****
End Sub

Private Sub bt1_Click()
    '设置窗体"退出"命令按钮标题为红色显示
    '***** Add2 *****
    Me.bt2.ForeColor = vbRed
    Me.bt2.ForeColor = RGB(255, 0, 0)
    '***** Add2 *****
    
    '预览方式输出报表rEmp
    '***** Add3 *****
    DoCmd.OpenReport "rEmp", acViewPreview, "", "", acNormal
    '***** Add3 *****
    '正常结束
    Exit Sub
    MsgBox Err.Description, vbCritical + vbOKOnly, "Error"
End Sub

引用表

Private Sub Command4_Click()
    BBB.Form.RecordSource = "select * from tStudent where 姓名 like '" & Me![Text2] & "*'"    
End Sub

Private Sub bList_Click()
'**************** 请在下面双引号内添入适当的SELECT语句 *************'
     BBB.Form.RecordSource = "SELECT * from fStudent;"
'*******************************************************************'
     [Text2] = " "
End Sub

for

Private Sub btnP_Click()
    Dim n As Integer  
    '计算1+2+3+…+n<=30000的最大n值
    '***** Add2 *****
    Dim sum As Integer
    sum = 0
    MsgBox (sum)
    For n = 1 To sum <= 30000
     sum = sum + n
     
   Next n
    n = sum
    
    '***** Add2 *****
    
    '将n的值显示在文本框"tData"内
    Me!tData = n
    
    '以下是文件操作
    If Dir(CurrentProject.Path & "\out.dat", vbDirection) <> vbNullString Then
       Kill CurrentProject.Path & "\out.dat"
    End If
    Open CurrentProject.Path & "\out.dat" For Output As #1
        Print #1, Me!tData
    Close #1

End Sub

设置标题

Private Sub Form_Load()
    '设置窗体标题为"****年度报表输出"
'***** Add1 *****
Me.Caption = Year(Date) & "年度报表输出"
Me.Caption = Year(Date) & "年度报表输出"
Me.Caption = Year(Date()) & "年度报表输出"
Me.Caption = Year(Date()) & "年度报表输出"

Form.Caption = Year(Date) & "年度报表输出"
Form.Caption = Year(Now) & "年度报表输出"
Form.Caption = Year(Now()) & "年度报表输出"

Me.bTitle.Caption = Year(Date) & "年度报表输出"
Me.bTitle.Caption = Year(Now) & "年度报表输出"
Me.bTitle.Caption = Year(Now()) & "年度报表输出"
Form.bTitle.Caption = Year(Now()) & "年度报表输出"
Form.bTitle.Caption = Year(Date) & "年度报表输出"
'***** Add1 *****
End Sub

Private Sub bt1_Click()

    '设置窗体"退出"命令按钮标题为红色显示
    '***** Add2 *****
    bt2.ForeColor = vbRed
    bt2.ForeColor = RGB(255, 0, 0)
    '***** Add2 *****
    
    '预览方式输出报表rEmp
    '***** Add3 *****
     DoCmd.OpenReport "rEmp", acViewPreview, "", "", acNormal
    DoCmd.OpenReport "rEmp", acViewNormal, "", "", acNormal
    '***** Add3 *****
    
    '正常结束
    Exit Sub
    
    MsgBox Err.Description, vbCritical + vbOKOnly, "Error"
    
End Sub

字体加粗

Private Sub Form_Load()
    '设置窗体标题为"**年度报表输出"
    '***** Add1 *****
    Me.Caption = Right(Year(Date), 2) & "年度报表输出"
    Me.Caption = Right(Year(Now), 2) & "年度报表输出"
    Me.bTitle.Caption = Right(Year(Date), 2) & "年度报表输出"
    Me.bTitle.Caption = Right(Year(Now), 2) & "年度报表输出"
    
	Me.Caption = Right(Year(Date()), 2) & "年度报表输出"
    Me.Caption = Right(Year(Now()), 2) & "年度报表输出"
    Me.bTitle.Caption = Right(Year(Date()), 2) & "年度报表输出"
    Me.bTitle.Caption = Right(Year(Now()), 2) & "年度报表输出"
    '***** Add1 *****
End Sub
Private Sub bt1_Click()   
    ''On Error GoTo ErrHanle    
    '设置窗体"退出"命令按钮标题为粗体显示
    '***** Add2 *****
    Me.bt2.FontBold = True
    Me.bt2.fontStorg = True
    '***** Add2 *****    
    '预览方式输出报表rEmp
    '***** Add3 *****
    DoCmd.OpenReport "rEmp", acViewPreview, "", "", acNormal
    '***** Add3 *****
    
    '正常结束
    Exit Sub
    
     '错误处理
    '***** Add4 *****
     ''ErrHanle
    '***** Add4 *****
    
    MsgBox Err.Description, vbCritical + vbOKOnly, "Error"
    
End Sub

5050

Private Sub btnP_Click()
    Dim n As Integer    
    '计算1+2+3+…+n<=30000的最大n值
    '***** Add2 *****
    Dim sum As Integer
    Dim s As Integer
    n = 0
    sum = 0
    Do While sum <= 30000
        n = n + 1
        sum = sum + n
    Loop
    n=n-1
	'方法二:因为不知道是sum和s
    n = 0
    s= 0
    Do While s <= 30000
        n = n + 1
        s = s + n
    Loop
    n = n - 1
    '***** Add2 *****    
    '将n的值显示在文本框"tData"内
    Me!tData = n
    
    '以下是文件操作
    If Dir(CurrentProject.Path & "\out.dat", vbDirection) <> vbNullString Then
       Kill CurrentProject.Path & "\out.dat"
    End If
    Open CurrentProject.Path & "\out.dat" For Output As #1
        Print #1, Me!tData
    Close #1

End Sub

Private Sub btnQ_Click()
    '代码调用宏对象"mEmp"
    '***** Add3 *****
    DoCmd.RunMacro "mEmp"
    '***** Add3 *****
End Sub

Private Sub Form_Load()
    '设置窗体标题为标签"bTitle"的标题内容
    '***** Add1 *****
    Me.Caption = "信息输出"
    Me.bTitle.Caption = "信息输出"
    Me.bTitle = "信息输出"
    '***** Add1 *****
End Sub

判断年龄下于30的人

Private Sub btnP_Click()
    Dim k As Integer
    Dim MAgeMax As Integer
    
    Dim cn As New ADODB.Connection
    Dim rs As New ADODB.Recordset
    Dim strSQL As String
    
    Set cn = CurrentProject.Connection
    strSQL = "select 性别,年龄 from tEmp where 性别='男'"
    rs.Open strSQL, cn, adOpenDynamic, adLockOptimistic
    
    MAgeMax = 0: k = 0
    
    '查找tEmp表中男性员工的年龄最大值并统计年龄在30以下的男员工人数
    Do While Not rs.EOF
        If rs.Fields("年龄") > MAgeMax Then
            '*****Add2*****
            MAgeMax = rs.Fields("年龄")
            '*****Add2*****
        End If
        
        '统计30岁以下男员工人数
        If rs.Fields("年龄") < 30 Then
            k = k + 1
        End If
        
        '记录集遍历
        '因为可能不会进入if,所以要这一句
        '*****Add3*****
        MAgeMax = rs.Fields("年龄")
        '*****Add3*****
    Loop

    rs.Close
    Set rs = Nothing
    
    '将男性员工最大年龄值显示在文本框"tData"内
    Me!tData = "最大年龄:" & MAgeMax
    
    '以下是文件操作保存n值
    If Dir(CurrentProject.Path & "\out.dat", vbDirection) <> vbNullString Then
       Kill CurrentProject.Path & "\out.dat"
    End If
    Open CurrentProject.Path & "\out.dat" For Output As #1
        Print #1, k
    Close #1

End Sub

Private Sub btnQ_Click()
    '代码调用宏对象"mEmp"
    '*****Add4*****
   DoCmd.RunMacro "mEmp"
    '*****Add4*****
End Sub

Private Sub Form_Load()
    '设置窗体标题为标签"bTitle"的标题内容
    '*****Add1*****
    Me.bTitle.Caption = "信息输出"
    Me.Caption = "信息输出"
    '*****Add1*****
End Sub

名称

Private Sub cItem_AfterUpdate()
'将标签显示内容设置为所选项目名加上"内容:"
'************************* Add1 *************************
Dim aa
aa = CItem.Value
Ldetail.Caption = CItem.Value + "内容:"
Ldetail.Caption = CItem.Value & "内容:"
Ldetail.Caption = aa + "内容:"
Ldetail.Caption = aa & "内容:"
'不知道冒号的字节
Ldetail.Caption = CItem.Value + "内容:"
Ldetail.Caption = CItem.Value & "内容:"
Ldetail.Caption = aa + "内容:"
Ldetail.Caption = aa & "内容:"

aa = CItem.Value
me.Ldetail.Caption = CItem.Value + "内容:"
me.Ldetail.Caption = CItem.Value & "内容:"
me.Ldetail.Caption = aa + "内容:"
me.Ldetail.Caption = aa & "内容:"
'不知道冒号的字节
me.Ldetail.Caption = CItem.Value + "内容:"
me.Ldetail.Caption = CItem.Value & "内容:"
me.Ldetail.Caption = aa + "内容:"
me.Ldetail.Caption = aa & "内容:"
'************************* Add1 *************************
Me.Refresh
End Sub

Private Sub CmdClear_Click()
  [TxtDetail] = " "
  [CItem] = " "
End Sub

Private Sub CmdList_Click()
'显示全部记录
'下面有提示
'************************* Add2 *************************
fDetail.Form.RecordSource = "tStud"
fDetail.Form.RecordSource = "select * from tStud"
'************************* Add2 *************************
End Sub

Private Sub CmdRefer_Click()
  If Trim(Me![TxtDetail]) <> "" And Trim(Me![CItem]) <> "" Then
     fDetail.Form.RecordSource = "select * from tStud where " & LTrim(Me.[CItem]) & " like '" & Me![TxtDetail] & "*'"
  Else
  '设置提示框
'************************* Add3 *************************
     MsgBox "查询项目或查询内容不能为空", vbOKOnly, "注意"
'************************* Add3 *************************
  End If
End Sub

Private Sub Form_Load()
'设置窗体标题
'************************* Add4 ************************
Me.Form.Caption = tTitle.Caption
Form.Caption = tTitle.Caption
me.Caption = tTitle.Caption
'************************* Add4 ************************
End Sub

打印预览

Private Sub Form_Load()
    '设置Label1标签为红色文本显示
    '*****Add1*****"
    Me.Label1.ForeColor = vbRed
    Me.Label1.ForeColor = RGB(255, 0, 0)
    '*****Add1*****"
End Sub

'预览输出
Private Sub Cmd1_Click()
    '*****Add2*****"
     DoCmd.OpenReport "rEmp", acViewPreview, "", "", acNormal
    DoCmd.OpenReport "rEmp", acViewNormal, "", "", acNormal
    mdPnt acViewPreview
   mdPnt acViewNormal
    '*****Add2*****"
End Sub

'打印输出
Private Sub Cmd2_Click()
    mdPnt acViewNormal
End Sub

'输出过程
Private Sub mdPnt(flag As Integer)
    '按照参数条件输出
    DoCmd.OpenReport "rEmp", flag
End Sub

标题

Private Sub Form_Load()
   '设置窗体标题为当前日期
   '*****Add*****
   Me.Caption = Date
   Me.Caption = Now
   
   Form.Caption = Date
   Form.Caption = Now
   
   me.form.Caption = Date
   me.form.Caption = Now
   
    Me.Caption = Date()
   Me.Caption = Now()
   
   Form.Caption = Date()
   Form.Caption = Now()
   
   me.form.Caption = Date()
   me.form.Caption = Now()
   '*****Add*****
End Sub

Private Sub bt1_Click()
    '*****Add*****
    '更改窗体名称
    Me.Caption = tText.Value
    Me.Caption = Me.tText.Value
    Form.Caption = tText.Value
    Form.Caption = Me.tText.Value
	me.Form.Caption = tText.Value
    me.Form.Caption = Me.tText.Value
    
    '更改标题名称
    Me.bTitle.Caption = Me.tText.Value
    Me.bTitle.Caption = tText.Value
    '*****Add*****
End Sub

返回表中记录的条数

Private Sub bt_Click()
    Dim cn As New ADODB.Connection
    Dim rs As New ADODB.Recordset
    
    Dim strSQL As String
    Dim sage As Single
        
    '设置当前数据库连接
    Set cn = CurrentProject.Connection
    '盲猜:strSQL = tEmp表中是党员的年龄
    strSQL = "select avg(年龄) from tEmp where 党员否"
    '盲猜:调用tEmp
    rs.Open strSQL, cn, adOpenDynamic, adLockOptimistic
    
    '*****Add1*****
    '盲猜:RecordCount返回rs中的记录条数
    If rs.RecordCount = 0 Then
    '*****Add1*****
        MsgBox "无党员职工的年龄数据"
        sage = 0
        Exit Sub
    Else
        sage = rs.Fields(0)
    End If
    
    '*****Add2*****
    tAge = sage
    '*****Add2*****
    
    rs.Close
    cn.Close
    Set rs = Nothing
    Set cn = Nothing
    
    '以下是外部文件写入操作
    If Dir(CurrentProject.Path & "\out.dat", vbDirection) <> vbNullString Then
       Kill CurrentProject.Path & "\out.dat"
    End If
    Open CurrentProject.Path & "\out.dat" For Output As #1
        Print #1, sage
    Close #1
End Sub

设置背景

Private Sub Form_Load()
   '设置窗体背景图片
   '*****Add*****
   '注意:反斜杆,可以试
   Form.Picture = CurrentProject.Path & "\test.jpg"
   Form.Picture = CurrentProject.Path + "\test.jpg"
   '*****Add*****
End Sub

素数

Private Function sushu(v As Integer) As Boolean
    Dim i As Integer
    For i = 2 To v - 1
        If v Mod i = 0 Then
            sushu = False
            Exit Function
        End If
    Next
    sushu = True
End Function

Private Sub btnP_Click()
    Dim n As Integer
    Dim mn As Integer
    Dim i As Integer
   '***** Add2 *****
   n = 0
   For i = 2 To 10000
    If sushu(i) And i > mn Then
        n = n + 1
        mn = i
    End If
   Next i

   
   n = 0
   For i = 2 To 10000
    If sushu(i) And mn < i Then
        n = n + 1
        mn = i
    End If
   Next i
   
   '方法2
   n = 0
   i = 2
   Do While i < 10000
    If sushu(i) And i > mn Then
        n = n + 1
        mn = i
    End If
    i = i + 1
   Loop
   
   n = 0
   i = 2
   Do While i < 10000
    If sushu(i) And mn < i Then
        n = n + 1
        mn = i
    End If
    i = i + 1
   Loop
    '***** Add2 *****
    tData.SetFocus
    tData.Text = CStr(n) & "," & CStr(mn)
End Sub

Private Sub btnQ_Click()
'代码调用宏对象"mEmp"
    '***** Add3 *****
   DoCmd.RunMacro "mEmp"
    '***** Add3 *****
End Sub

Private Sub Form_Current()
'设置窗体标题为标签"bTitle"的标题内容
'***** Add1 *****
Me.Caption = "信息输出"
Me.Caption = bTitle.Caption
Form.Caption = "信息输出"
Form.Caption = bTitle.Caption
Me.Caption = "信息输出"
Me.Caption = bTitle.Caption
me.Form.Caption = "信息输出"
me.Form.Caption = bTitle.Caption
'***** Add1 *****
End Sub

trim

Private Sub bt1_Click()
'*****Add1*****
Private Sub bt1_Click()
'*****Add1*****
bTitle.Caption = Trim(tText.Value) & "等级考试测试"
bTitle.Caption = Trim(tText.Value) + "等级考试测试"

bTitle.Caption = Trim(tText) & "等级考试测试"
bTitle.Caption = Trim(tText) + "等级考试测试"
'*****Add1*****
End Sub
'*****Add1*****
End Sub

按钮的使用

Private Sub CmdEdit_Click()
      tUser_1.Enabled = True
      Me!Lremark.Visible = True
      Me!tPass.Visible = True
      Me!tRemark_1.Visible = True
      Me!tEnter.Visible = True
      CmdSave.Enabled = True
      Me!tEnter = ""
End Sub

Private Sub cmdquit_Click()
' *********************** Add4 *************************************
        DoCmd.Close
' *********************** Add4 *************************************
End Sub
Private Sub CmdSave_Click()
  If Me!tPass = Me!tEnter Then
    DoCmd.RunSQL ("update tUser " & "set 用户名='" & Me!tUser_1 & "'" & "where 用户名='" & Me!tUser_1 & "'")
    DoCmd.RunSQL ("update tUser " & "set 口令='" & Me!tPass & "'" & "where 用户名='" & Me!tUser_1 & "'")
    DoCmd.RunSQL ("update tUser " & "set 备注='" & Me!tRemark_1 & "'" & "where 用户名='" & Me!tUser_1 & "'")
    Forms!fEdit.Refresh
    DoCmd.GoToControl "cmdedit"
' *********************** Add1 *************************************
    CmdSave.Enabled = True
' *********************** Add1 *************************************
    Me!tUser_1 = Me!用户名
    Me!tPass = Me!口令
    Me!tRemark_1 = Me!备注
    Me!tEnter = " "
    Me!tPass.Visible = False
    Me!tRemark_1.Visible = False
    Me!tEnter.Visible = False
    Me!Lremark.Visible = False
' *********************** Add2 *************************************
    CmdSave.Enabled = False
' *********************** Add2 *************************************
  Else
' *********************** Add3 *************************************
        MsgBox "请重新输入口令!", vbOKOnly
        MsgBox "请重新输入口令!", vbOKquit
' *********************** Add3 *************************************
  End If
End Sub

使用定义

Private Sub cmdEnter_Click()
    Dim name As String, pass As String
    name = Nz(Me!tUser)
    pass = Nz(Me!tPass)
'注意:因为上面定义了,所以要使用
' *********************** Add1 *************************************
If name = "cueb" And pass = "1234" Then
' *********************** Add1 *************************************
        MsgBox "密码输入正确,欢迎进入系统!", vbOKOnly + vbCritical, "欢迎"  '显示消息框
        DoCmd.Close
    Else
        MsgBox "密码错误!", vbOKOnly   '显示消息框
        Me!tUser = ""       '使文本框清空
        Me!tPass = ""
' *********************** Add2 *************************************
        tUser.SetFocus
' *********************** Add2 *************************************
    End If
End Sub

标题

Private Sub Form_Load()
    '设置窗体标题为"**年度报表输出"
    '***** Add1 *****
    Form.Caption = Right(Year(Date), 2) & "年度报表输出"
    Form.Caption = Right(Year(Date), 2) + "年度报表输出"
    Me.Caption = Right(Year(Date), 2) & "年度报表输出"
    Me.Caption = Right(Year(Date), 2) + "年度报表输出"
       
    Form.Caption = Right(Year(Now), 2) & "年度报表输出"
    Form.Caption = Right(Year(Now), 2) + "年度报表输出"
    Me.Caption = Right(Year(Now), 2) & "年度报表输出"
    Me.Caption = Right(Year(Now), 2) + "年度报表输出"
    
    Form.Caption = Right(Year(Now()), 2) & "年度报表输出"
    Form.Caption = Right(Year(Now()), 2) + "年度报表输出"
    Me.Caption = Right(Year(Now()), 2) & "年度报表输出"
    Me.Caption = Right(Year(Now()), 2) + "年度报表输出"
    
    Me.bTitle.Caption = Right(Year(Date), 2) & "年度报表输出"
    Me.bTitle.Caption = Right(Year(Date), 2) + "年度报表输出"
    
    Me.bTitle.Caption = Right(Year(Now), 2) & "年度报表输出"
    Me.bTitle.Caption = Right(Year(Now), 2) + "年度报表输出"
    Me.bTitle.Caption = Right(Year(Now()), 2) & "年度报表输出"
    Me.bTitle.Caption = Right(Year(Now()), 2) + "年度报表输出"
    '***** Add1 *****
End Sub

Private Sub bt1_Click()
    
    ''On Error GoTo ErrHanle
    
    '设置窗体"退出"命令按钮标题为粗体显示
    '***** Add2 *****
    bt2.FontBold = True
    '***** Add2 *****
    
    '预览方式输出报表rEmp
    '***** Add3 *****
    DoCmd.OpenReport "rEmp", acViewPreview, "", "", acNormal
    DoCmd.OpenReport "rEmp", acViewNormal, "", "", acNormal
    '***** Add3 *****
    
    '正常结束
    Exit Sub
    
     '错误处理
    '***** Add4 *****
     ''ErrHanle
    '***** Add4 *****
    
    MsgBox Err.Description, vbCritical + vbOKOnly, "Error"
    
End Sub

设置标题

Private Sub bC_Click()
CDID_标签.ForeColor = vbRed
CDID_标签.ForeColor = RGB(255, 0, 0)

Me.CDID_标签.ForeColor = vbRed
Me.CDID_标签.ForeColor = RGB(255, 0, 0)

Me.Form.CDID_标签.ForeColor = vbRed
Me.Form.CDID_标签.ForeColor = RGB(255, 0, 0)

CDID.ForeColor = vbRed
CDID.ForeColor = RGB(255, 0, 0)

Me.CDID.ForeColor = vbRed
Me.CDID.ForeColor = RGB(255, 0, 0)

Me.Form.CDID.ForeColor = vbRed
Me.Form.CDID.ForeColor = RGB(255, 0, 0)
End Sub

总结

  • 有inputbox,就需要msgbox("你好")
  • 设置标题可用id.属性form.属性me.属性me.form.属性
  • 时间可用datedate()nownow()
  • 先看有没有定义,有定义用定义
  • 设置标签颜色id.fontColor = vbredid.fontColor = rgb(255,0,0)me.id.fontColor = vbred
  • Select没有匹配的就执行Case Is >= 3
  • 窗体数据来自表(tStud)中的数据:fDetail.Form.RecordSource = "select * from tStud;"
  • 设置窗体背景图片:Picture = CurrentProject.Path & "\test.jpg",Picture = CurrentProject.Path + "\test.jpg"
  • 指定窗体的数据源Form.RecordSource = "formName"
  • '提示框有“是”和“否”两个选项MsgBox "报表预览", vbQuestion + vbYesNo, "确认"
  • 获取用户的点击:a = MsgBox("报表预览", vbQuestion + vbYesNo, "确认"),If a = vbYes Then
  • 只有确定按钮MsgBox "请重新输入口令!", vbOkquitMsgBox "请重新输入口令!", vbOkOnly
  • inputbox,就有msgbox "你好"
  • Trim删除字符串前面和后面的空格:Trim(tText.Value) & "等级考试测试"
  • 判断表中是否有相同的数据:name.EOFnot name.EoF
  • '将光标移到tUser处:tUser.SetFocus
  • 禁用按钮: CmdSave.Enabled = FalseCmdSave.Enabled = True
  • 标题为粗体:Me.id.FontBold = TrueMe.id.FontBold = False
  • 运行宏: DoCmd.RunMacro "MacroName"
  • RecordCount返回rs中的记录条数:rs.RecordCount