《VBA程序设计范例.doc》由会员分享,可在线阅读,更多相关《VBA程序设计范例.doc(12页珍藏版)》请在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 10
4、0 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 To
5、 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.Set
6、Focusa2 = 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.SetFocusT
8、ext1.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.G
9、oToRecord , , 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 Res
10、ume 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.Go
11、ToRecord , , 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.Set
12、Focusstudname = Trim(Text2.Text)If studname = Space(0) Then MsgBox 请输入学生姓名!, vbOKOnlyElse Me.Child8.RequeryEnd IfEnd Sub拓展2:窗体查询2源代码:SQL查询部分:SELECT 学生学籍.班级编号, 学生学籍.学号, 学生学籍.姓名, 学生成绩.学期, 学生成绩.语文, 学生成绩.数学, 学生成绩.英语, 学生成绩.专业一, 学生成绩.专业二, 学生成绩.总分FROM 学生学籍 INNER JOIN 学生成绩 ON (学生学籍.学号 = 学生成绩.学号) AND (学生学籍.班
13、级编号 = 学生成绩.班级编号)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 Sub12设 解 . ,!姓入 0 = ( ( 码代击按查 象对查对源子 .窗_
14、窗! 姓 号级绩学=号班学 )绩 =籍学 生 生 总绩生二专生学业成 英.生数成 语.学学成 名学学学学生号.生 分部 码询窗 ,名生入 ) = ( _ 码代击钮查 象询为象的子 * 子主查 籍籍 * 分询 码询窗拓拓 _ _ , . _ _ ( _ _ . _ , _ ( _ _ . _ , ( _ _ _ ( 码问本基设体 , ) = ( = = ( . , ) 数个 &第请 =( 0 ( 码面数组序升按组一: , ) . . ( ) = ( ) = = =) ( =( ) ( 码面数这出排请 0 , ,:有 ( ( = ( ( 0 ) ( = ) =( =( ( 0 ( 码面值)(数出,, , 数一拓拓 于 于等 . 于 . = ( 数较个请 0 0 ( . . ( 0 ( 码小其比两输用问本问序定的型数内00* 问相 . . = = = = ( 码) (+ +( + + 编 = . = = 0 = ( 码面)0+ + ) ( 程 = . + 数的于入 ) ,入 . . ( 码面 + 算编 0 改修码00+计程 += = 改修 + 算:拓题问 范序 ( . += = ( 码面0 +算程问本题和