《VBA程序设计范例(13页).doc》由会员分享,可在线阅读,更多相关《VBA程序设计范例(13页).doc(14页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、-高考VBA程序设计解析一、累加和问题:1、基本问题问题:编程计算1+2+3+100;程序界面:源代码:Private Sub Command3_Click()Dim s As IntegerDim i As Integers = 0For i = 1 To 100 Step 1 s = s + iNext iText1.SetFocusText1.Value = sEnd SubPrivate Sub Command4_Click()DoCmd.Close acForm, VBA程序设计范例1End Sub2、问题拓展拓展1:编程计算1+3+5+7+99;源代码修改:For i = 1 To
2、 99 Step 2 s = s + iNext i拓展2:编程计算2+4+6+8+100;源代码修改:For i = 2 To 100 Step 2 s = s + iNext i拓展3:编程计算1+2+3+N;程序界面:源代码:Private Sub Command10_Click()Dim i As IntegerDim s As IntegerText3.SetFocusIf Trim(Text3.Text) = Space(0) Then MsgBox 请输入N的值!, vbOKOnly Exit SubElseIf Val(Text3.Text) 1 Then MsgBox 请输入
3、大于1的正整数!, vbOKOnly Exit SubEnd Ifs = 0For i = 1 To Val(Text3.Value) Step 1 s = s + iNext iText8.SetFocusText8.Value = sEnd Sub拓展4:编程计算1+(1+2)+(1+2+3)+(1+2+3+4)+(1+2+3+4+ +100);程序界面:源代码:Private Sub Command3_Click()Dim i As Integer, j As IntegerDim s1 As Integer, s2 As Doubles1 = 0s2 = 0For i = 1 To 1
4、00 Step 1 s1 = 0 For j = 1 To i s1 = s1 + j Next j s2 = s2 + s1Next iText1.SetFocusText1.Text = s2End Sub拓展5:编程计算1+(1+3)+(1+3+5)+(1+3+5+7)+(1+3+5+7+ +99);源代码:Private Sub Command3_Click()Dim i As Integer, j As IntegerDim s1 As Integer, s2 As Doubles1 = 0s2 = 0For i = 1 To 50 Step 1 s1 = 0 For j = 1 T
5、o i*2-1 step 2 s1 = s1 + j Next j s2 = s2 + s1Next iText1.SetFocusText1.Text = s2End Sub3、算法相关问题编程计算1*2*3* *100;提示:内存变量的数据类型的确定;二、排序问题1、基本问题问题:提示用户输入两个数,比较其大小;源代码:Private Sub Command4_Click()Dim a1 As StringDim a2 As StringDim b1 As IntegerDim b2 As IntegerText0.SetFocusa1 = Trim(Text0.Text)Text2.Se
6、tFocusa2 = Trim(Text2.Text)If a1 = Space(0) Or a2 = Space(0) Then MsgBox 请输入两个待比较的数!, vbOKOnly Exit SubEnd Ifb1 = Val(a1)b2 = Val(a2)Text8.SetFocusIf b1 maxvalue Then maxvalue = arr(i) End If If arr(i) arr(j) Then temp = arr(i) arr(i) = arr(j) arr(j) = temp End If Next jNext iText2.SetFocusFor i = 1
7、 To 10 Text2.Text = Text2.Text & arr(i) & ,Next iEnd Sub拓展3:输入一组数, 请按升序排序输出这组数;程序界面:源代码:Private Sub Command6_Click()Dim arr(1 To 10) As IntegerDim i As IntegerDim j As IntegerDim temp As IntegerDim s1 As StringFor i = 1 To 10 arr(i) = InputBox(请输入第 & i & 个数:) s1 = s1 & arr(i) & ,Next iText1.SetFocus
8、Text1.Value = s1For i = 1 To 9 For j = i + 1 To 10 If arr(i) arr(j) Then temp = arr(i) arr(i) = arr(j) arr(j) = temp End If Next jNext iText3.SetFocusFor i = 1 To 10 Text3.Text = Text3.Text & arr(i) & ,Next iEnd Sub三、窗体设计1、基本问题源代码:Private Sub Command11_Click()On Error GoTo Err_Command11_Click DoCmd.
9、GoToRecord , , acFirstExit_Command11_Click: Exit SubErr_Command11_Click: MsgBox Err.Description Resume Exit_Command11_ClickEnd SubPrivate Sub Command12_Click()On Error GoTo Err_Command12_Click DoCmd.GoToRecord , , acPreviousExit_Command12_Click: Exit SubErr_Command12_Click: MsgBox Err.Description Re
10、sume Exit_Command12_ClickEnd SubPrivate Sub Command13_Click()On Error GoTo Err_Command13_Click DoCmd.GoToRecord , , acNextExit_Command13_Click: Exit SubErr_Command13_Click: MsgBox Err.Description Resume Exit_Command13_ClickEnd SubPrivate Sub Command14_Click()On Error GoTo Err_Command14_Click DoCmd.G
11、oToRecord , , acLastExit_Command14_Click: Exit SubErr_Command14_Click: MsgBox Err.Description Resume Exit_Command14_ClickEnd Sub2、问题拓展拓展1:窗体查询1源代码:SQL查询部分:SELECT * FROM 学生学籍 WHERE 姓名 like forms!窗体查询_主子窗体1.text2.text;子窗体的源对象设置为查询对象;查询按钮单击事件代码:Private Sub Command6_Click()Dim studname As StringText2.Se
12、tFocusstudname = Trim(Text2.Text)If studname = Space(0) Then MsgBox 请输入学生姓名!, vbOKOnlyElse Me.Child8.RequeryEnd IfEnd Sub拓展2:窗体查询2源代码:SQL查询部分:SELECT 学生学籍.班级编号, 学生学籍.学号, 学生学籍.姓名, 学生成绩.学期, 学生成绩.语文, 学生成绩.数学, 学生成绩.英语, 学生成绩.专业一, 学生成绩.专业二, 学生成绩.总分FROM 学生学籍 INNER JOIN 学生成绩 ON (学生学籍.学号 = 学生成绩.学号) AND (学生学籍.班级编号 = 学生成绩.班级编号)WHERE 姓名 like forms!窗体查询_主子窗体2.text2.text;子窗体的源对象设置为查询对象;查询按钮单击事件代码:Private Sub Command6_Click()Dim studname As StringText2.SetFocusstudname = Trim(Text2.Text)If studname = Space(0) Then MsgBox 请输入学生姓名!, vbOKOnlyElse Me.Child8.RequeryEnd IfEnd Sub第 14 页-