to wshlxvb(酒鬼英明) :你说的属性好像是DAO的,或ADOX的吧!
可能我没说明白,我是用VB做个对ACCESS数据库的维护程序,要对字段添加说明,用ADO实在是没办法了,哪位要是有用DAO添加说明即.Description的属性,发到我的信箱也行啊!立刻放分。谢了。
zhangruijilin@yahoo.com.cn
用 ACCESS 作为多用户的服务器数据库,需要引入 事务,负责会容易产生数据不完整!
以下例子是用 ADO 连接 SQL SERVER 和 ACCESS 数据库的例子:
Private Sub Command1_Click()
On Error GoTo errorHandler
Dim adoCnn As New ADODB.Connection
Dim adoRst As New ADODB.Recordset
Dim BeginTransBol As Boolean '是否已开始一个 ado 事务
Dim CnnStr As String
Dim cSql As String
BeginTransBol = False
'CnnStr = "Provider=SQLOLEDB.1;Persist Security Info=True; Initial Catalog=[数据库名];Data Source=[服务器名]; User ID=sa;Password=;" '基于 用户名和密码的访问
'CnnStr = "Provider=SQLOLEDB.1;Persist Security Info=False;Initial Catalog=[数据库名];Data Source=[服务器名]; Integrated Security=SSPI;" '基于 windows 集成的安全访问
CnnStr = "provider=Microsoft.Jet.OLEDB.4.0;Data source =" + [Access97、Access2000 数据库路径及名称] + " ;Persist Security Info=False;Jet OLEDB:Database Password=" + [数据库密码] '访问 Access 数据库
With adoCnn
If .State = adStateOpen Then .Close
.ConnectionString = CnnStr
.Open
If .State = adStateOpen Then
' 数据库打开成功
' 开始一个事物,设置事务的隔离层为 adXactReadCommitted
cSql = "SELECT * FROM report_head1 WHERE 编号 = " + Text1.Text
With adoRst
If .State = adStateOpen Then .Close
.ActiveConnection = adoCnn
.CursorLocation = adUseClient
.LockType = adLockBatchOptimistic
.CursorType = adOpenStatic
' 当查询为多个表的联合查询同时更新只对其中的一个表进行时,可用下面的语句显式指定要更新的表名
.Properties("Unique Table") = "report_head1"
.Source = cSql
.Open
' Disconnect the recordset 切断记录集
Set .ActiveConnection = Nothing
If .RecordCount <> 0 Then
MsgBox "编号已经存在,请重新输入!", vbInformation, "录入重复. . ."
Text1.SetFocus
Else
.AddNew
'开始添加记录
.Fields("编号").Value = Text1.Text
'...
' Reconnect and update Access or SQL Server
.ActiveConnection = adoCnn
.UpdateBatch
End If
End With
.CommitTrans '提交当前事务
BeginTransBol = False
Else
' 数据库打开失败
' ...
End If
End With
Exit Sub
errorHandler:
If adoCnn.State = adStateOpen And BeginTransBol Then adoCnn.RollbackTrans '有错误发生,回滚当前事务
MsgBox Err.Description + "(错误号:" + CStr(Err.Number) + ")", vbCritical, "出错拉 . . ."
End Sub
Private Sub print_kuozhan_Click()
'按扩展方式进行打印
On Error GoTo errorHandler
Call SetCursor(LoadCursor(0, 32514))
Dim Idnamestr As String
Dim cSql As String
Dim strYear As String '系统时间的年月日
Dim strtime As String '系统时间的时分
Dim str As String '包含年月日和时分的系统时间
Dim BeginTransBol As Boolean '是否已开始一个 ado 事务
With adoCnn
If .State <> adStateOpen Then
If Not vConnection_ADO(adoCnn, MainDataName, , PasswordM) Then
MsgBox "ADO 没有正常连接上数据库,请关闭当前窗体再打开一次试试。" + vbCrLf + "如果还是报同样的错误,请与系统管理员联系!", vbCritical, MsgBoxTitle
Exit Sub
End If
End If
This example tries to set the value of a user-defined property. If the property doesn't exist, it uses the CreateProperty method to create and set the value of the new property. The SetProperty procedure is required for this procedure to run.
Sub CreatePropertyX()
Dim dbsNorthwind As Database
Dim prpLoop As Property
Set dbsNorthwind = OpenDatabase("Northwind.mdb")
' Set the Archive property to True.
SetProperty dbsNorthwind, "Archive", True
With dbsNorthwind
Debug.Print "Properties of " & .Name
' Enumerate Properties collection of the Northwind
' database.
For Each prpLoop In .Properties
If prpLoop <> "" Then Debug.Print " " & _
prpLoop.Name & " = " & prpLoop
Next prpLoop
' Delete the new property since this is a
' demonstration.
.Properties.Delete "Archive"
.Close
End With
End Sub
Sub SetProperty(dbsTemp As Database, strName As String, _
booTemp As Boolean)
Dim prpNew As Property
Dim errLoop As Error
' Attempt to set the specified property.
On Error GoTo Err_Property
dbsTemp.Properties("strName") = booTemp
On Error GoTo 0
Exit Sub
Err_Property:
' Error 3270 means that the property was not found.
If DBEngine.Errors(0).Number = 3270 Then
' Create property, set its value, and append it to the
' Properties collection.
Set prpNew = dbsTemp.CreateProperty(strName, _
dbBoolean, booTemp)
dbsTemp.Properties.Append prpNew
Resume Next
Else
' If different error has occurred, display message.
For Each errLoop In DBEngine.Errors
MsgBox "Error number: " & errLoop.Number & vbCr & _
errLoop.Description
Next errLoop
End
End If