Access开发培训
网站公告
·Access专家课堂QQ群号:151711184    ·Access快速开发平台下载地址及教程    ·欢迎加入Access专家课堂微信群!    ·如何快速搜索本站文章|示例|资料    
您的位置: 首页 > 技术文章 > Access数据库-模块/函数/VBA

access-VBA编程 第四章 数据输入、查询、计算、连接1

时 间:2009-02-16 08:30:03
作 者:UMVsoft整理   ID:1445  城市:上海
摘 要:VBA


正 文:

第四章 数据输入、查询、计算、连接1

通过英特网的ACCESS联接
在ACCESS中使用ADO:
Private Sub ABC_Click()
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
cn.OPEN "DSN=alwin;UID=;PWD=;"
rs.OPEN "Select * from tbTABLE", cn, adOpenDynamic, adLockReadOnly’
rs.ABC App.Path & "\testdata.dat", adPersistADTG
rs.Close
cn.Close
MsgBox ("OPERATION OK")
End Sub
Private Sub OPEN_Click()
Dim strConnect As String
strConnect = "Provider=MSPersist"
Dim rs As New ADODB.Recordset
rs.OPEN "http://远程服务器的IP/test/testdata.dat", strConnect
Do While Not rs.EOF
Debug.Print rs("USERID").value
rs.MoveNext
Loop
End Sub
将用户输入的身份证号15位数据转化为18位。
Function IDCode15to18(sCode15 As String) As String
'* 功能:将15的身份证号升为18位(根据GB 11643-1999)
'* 参数:原来的号码
'* 返回:升位后的18位号码
Dim i As Integer
Dim num As Integer
Dim code As String
num = 0
IDCode15to18 = Left(sCode15, 6) + "19" + Right(sCode15, 9)
' 计算校验位
For i = 18 To 2 Step -1
num = num + (2 ^ (i - 1) Mod 11) * (Mid(IDCode15to18, 19 - i, 1))
Next i
num = num Mod 11
Select Case num
Case 0
code = "1"
Case 1
code = "0"
Case 2
code = "X"
Case Else
code = Trim(Str(12 - num))
End Select
IDCode15to18 = IDCode15to18 + code
End Function
据身份证号自动输入出生日期
Dim Length As Integer
Length = Len(Me.[身份证号])
If Not IsNull(Length) Then
If Length = 15 Then
Me.[性别] = IIf(Val(Mid(Me.身份证号, 15, 1)) / 2 = Int(Val(Mid(Me.身份证号, 15, 1)) / 2), "女", "男")
Me.[出生日期] = "19" & Mid([身份证号], 7, 2) & "-" & Mid([身份证号], 9, 2) & "-" & Mid([身份证号], 11, 2)
ElseIf Length = 18 Then
Me.[性别] = IIf(Val(Mid(Me.身份证号, 17, 1)) / 2 = Int(Val(Mid(Me.身份证号, 17, 1)) / 2), "女", "男")
Me.[出生日期] = Mid([身份证号], 7, 4) & "-" & Mid([身份证号], 11, 2) & "-" & Mid([身份证号], 13, 2)
Else
MsgBox "身份证号错误!"
End If
End If
两行代码打开另一数据库
Private Sub 命令4_Click()
On Error GoTo Err_命令4_Click
Dim strDb As String
strDb = "C:\db1.mdb"
SendKeys "{F11}%FO" & strDb & "{enter}"
Exit_命令4_Click:
Exit Sub
Err_命令4_Click:
MsgBox Err.Description
Resume Exit_命令4_Click
End Sub
实现打开外部数据库中的报表。
Private Declare Function apiSetForegroundWindow Lib "user32" _
Alias "SetForegroundWindow" _
(ByVal hwnd As Long) _
As Long
Private Declare Function apiShowWindow Lib "user32" _
Alias "ShowWindow" _
(ByVal hwnd As Long, _
ByVal nCmdShow As Long) _
As Long
Private Const SW_MAXIMIZE = 3
Private Const SW_NORMAL = 1
Function fOpenRemoteReport(strMDB As String, strReport As String, _
Optional intView As Variant) _
As Boolean
' strMDB: 外部数据库名称(含路径)
' strReport: 报表名称
' intView: 报表的打开方式
Dim objAccess As Access.Application
Dim lngRet As Long
On Error GoTo fOpenRemoteReport_Err
If IsMissing(intView) Then intView = acViewPreview
If Len(Dir(strMDB)) > 0 Then
Set objAccess = New Access.Application
With objAccess
lngRet = apiSetForegroundWindow(.hWndAccessApp)
lngRet = apiShowWindow(.hWndAccessApp, SW_NORMAL)
' 第一次调用ShowWindow似乎不做任何事情
lngRet = apiShowWindow(.hWndAccessApp, SW_NORMAL)
.OpenCurrentDatabase strMDB
.DoCmd.OpenReport strReport, intView
Do While Len(.CurrentDb.Name) > 0
DoEvents
Loop
End With
End If
fOpenRemoteReport_Exit:
On Error Resume Next
objAccess.Quit
Set objAccess = Nothing
Exit Function
fOpenRemoteReport_Err:
fOpenRemoteReport = False
Select Case Err.Number
Case 7866:
' mdb 已经被用独占方式打开
MsgBox "该数据库:" & strMDB & _
vbCrLf & "已经被用独占方式打开!" & vbCrLf _
& vbCrLf & "请重新用共享方式打开,再试一次!", _
vbExclamation + vbOKOnly, "不能打开数据库"
Case 2103:
' 报表不存在
MsgBox "在这个" & strMDB & "数据库中不存在该报表:" & strReport & _
vbCrLf & vbCrLf , _
vbExclamation + vbOKOnly, "报表不存在"
Case 7952:
' 用户关闭了这个 mdb
fOpenRemoteReport = True
Case Else:
MsgBox "错误#: " & Err.Number & vbCrLf & Err.Description, _
vbCritical + vbOKOnly, "运行时错误"
End Select
Resume fOpenRemoteReport_Exit
End Function

上一节 下一节



Access软件网QQ交流群 (群号:54525238)       Access源码网店

常见问答:

技术分类:

相关资源:

专栏作家

关于我们 | 服务条款 | 在线投稿 | 友情链接 | 网站统计 | 网站帮助