如何在VB中用ACCES建立库
各位大虾!我想用ACCESS做数据库可是又不知道详细的步骤以及操作数据库语句的写法,最好打包后不用配置ODBC请帮忙谢谢! 问题点数:100、回复次数:8Top
1 楼Fanks(铁面人)回复于 2002-02-25 09:24:46 得分 0
那最好用ADOTop
2 楼kmcyz(阿成)回复于 2002-02-25 09:32:48 得分 0
数据库最好用ACCESS直接建立,ODBC配置可用InstallShield等安装程序工具实现。Top
3 楼tief(但求中庸)回复于 2002-02-25 09:33:17 得分 30
在使用下面例子之前要进行DAO的引用。
CreateDatabase Method Example
This example uses CreateDatabase to create a new, encrypted Database object.
Sub CreateDatabaseX()
Dim wrkDefault As Workspace
Dim dbsNew As DATABASE
Dim prpLoop As Property
' Get default Workspace.
Set wrkDefault = DBEngine.Workspaces(0)
' Make sure there isn't already a file with the name of
' the new database.
If Dir("NewDB.mdb") <> "" Then Kill "NewDB.mdb"
' Create a new encrypted database with the specified
' collating order.
Set dbsNew = wrkDefault.CreateDatabase("NewDB.mdb", _
dbLangGeneral, dbEncrypt)
With dbsNew
Debug.Print "Properties of " & .Name
' Enumerate the Properties collection of the new
' Database object.
For Each prpLoop In .Properties
If prpLoop <> "" Then Debug.Print " " & _
prpLoop.Name & " = " & prpLoop
Next prpLoop
End With
dbsNew.Close
End Sub
Top
4 楼tief(但求中庸)回复于 2002-02-25 09:42:03 得分 30
对于DSN的建立,也可以使用代码实现:
下面是我以前做的一个例子:
.bas文件的内容:
'--------------------------------- Structure -----------------------------------------------
Type FileTime
dwLowDateTime As Long
dwHighDateTime As Long
End Type
'--------------------------------- Const ---------------------------------------------------
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const DSN_LIST = "SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources"
Public Const SUPPOSE_SIZE = 1024&
Public Const KEY_ENUMERATE_SUB_KEYS = &H8
Public Const KEY_QUERY_VALUE = &H1
Public Const STANDARD_RIGHTS_ALL = &H1F0000
Public Const KEY_SET_VALUE = &H2
Public Const KEY_CREATE_SUB_KEY = &H4
Public Const KEY_NOTIFY = &H10
Public Const KEY_CREATE_LINK = &H20
Public Const SYNCHRONIZE = &H100000
Public Const KEY_ALL_ACCESS = _
((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or _
KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or KEY_CREATE_LINK) And (Not SYNCHRONIZE))
Public Const ODBC_ADD_DSN = 1
Public Const ODBC_CONFIG_DSN = 2
Public Const ODBC_REMOVE_DSN = 3
'--------------------------------- API -----------------------------------------------------
Public Declare Function RegCloseKey Lib "advapi32.dll" _
(ByVal hKey As Long) As Long
Public Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias _
"RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, _
ByVal ulOptions As Long, ByVal samDesired As Long, _
phkResult As Long) As Long
Public Declare Function RegEnumValue Lib "advapi32.dll" Alias _
"RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, _
ByVal lpValueName As String, lpcbValueName As Long, lpReserved As Long, _
lpType As Long, lpData As Long, lpcbData As Long) As Long
Public Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" _
(ByVal hwndParent As Long, ByVal fRequest As Long, _
ByVal lpszDriver As String, ByVal lpszAttributes As String) _
As Long
'-------------------------------------------------------------------------------------------
.frm文件的内容
Option Explicit
'------------------------------------------------------------------------------------------
'
' Read DSN from registry, refresh the listbox
Private Sub RefreshDSNList()
Dim hReg As Long
Dim lRet As Long
Dim lLoop As Long
Dim lTypeInfo As Long
Dim lSizeDsn As Long
Dim strDsn As String * SUPPOSE_SIZE
Dim ft As FileTime
' Open the register key for dsn list
lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, DSN_LIST, ByVal 0&, KEY_ALL_ACCESS, hReg)
Debug.Print "hReg = " & hReg
If lRet Then
MsgBox "Can not get DSN infomation!", vbInformation, "ERROR"
Exit Sub
End If
lstDsn.Clear
' Enum the sub-keys, add key name to listbox
Do While True
lSizeDsn = SUPPOSE_SIZE
strDsn = Space(SUPPOSE_SIZE)
ft.dwHighDateTime = 0
ft.dwLowDateTime = 0
lRet = RegEnumValue(hReg, lLoop, strDsn, lSizeDsn, ByVal 0&, lTypeInfo, ByVal 0&, ByVal 0&)
Debug.Print "lRet = " & lRet
Debug.Print "strDsn = " & strDsn
If lRet Then
Exit Do
Else
lLoop = lLoop + 1
lstDsn.AddItem strDsn
End If
Loop
RegCloseKey hReg
End Sub
'------------------------------------------------------------------------------------------
'
' Add a system dsn named TEST ( SQL Server )
Private Sub cmdAddDsn_Click()
Dim lRet As Long
Dim strDriver As String
Dim strAttributes As String
strDriver = "SQL Server"
strAttributes = "SERVER=SERVER1" & Chr$(0)
strAttributes = strAttributes & "DESCRIPTION=MY DSN" & Chr$(0)
strAttributes = strAttributes & "DSN=TEST" & Chr$(0)
strAttributes = strAttributes & "DATABASE=mydata" & Chr$(0)
strAttributes = strAttributes & "UID=sa" & Chr$(0)
strAttributes = strAttributes & "PWD=" & Chr$(0)
lRet = SQLConfigDataSource(hWnd, ODBC_ADD_DSN, strDriver, strAttributes)
Debug.Print "lRet = " & lRet
If lRet Then
MsgBox "New DSN has been created.", vbInformation, "Message"
Else
MsgBox "DSN Creation Failed.", vbInformation, "ERROR"
End If
Call RefreshDSNList
End Sub
'------------------------------------------------------------------------------------------
'
' Remove the system dsn - test. If the DSN does not exists, msgbox err
Private Sub cmdRemoveDsn_Click()
Dim lRet As Long
Dim strDriver As String
Dim strAttributes As String
strDriver = "SQL Server"
strAttributes = "DSN=DSN_TEMP" & Chr$(0)
lRet = SQLConfigDataSource(0, ODBC_REMOVE_DSN, strDriver, strAttributes)
If lRet Then
MsgBox "My DSN has been deleted.", vbInformation, "Message"
Else
MsgBox "Can not delete the DSN!", vbInformation, "ERROR"
End If
Call RefreshDSNList
End Sub
'------------------------------------------------------------------------------------------
'
' Init the DSN list. add other initial here
Private Sub Form_Load()
Call RefreshDSNList
End Sub
Top
5 楼lanman(lanman)回复于 2002-02-25 10:20:39 得分 10
用ADO非常方便
dim cat as new adox.catalog
cat.Tables.Append aaa
cat.Tables("aaa").Columns.Append ???
具体看看MSDN吧。语法记不住了。Top
6 楼dbcontrols(泰山__抛砖引玉)回复于 2002-02-25 10:57:36 得分 10
http://www.wzjcw.net/vbgood/taishan/index.html有代码可以参考Top
7 楼yell(我)回复于 2002-02-25 11:27:56 得分 20
ADO好像不能建立Access库,得用DAO
On Error GoTo ErrHandler
Public gws As Workspace
Public gdb As Database
Dim tb As TableDef
Dim fld As Field
Dim idx As Index
Set gdb = DBEngine.Workspaces(0).CreateDatabase(App.Path + "\CheckData.ecd", dbLangGeneral, dbEncrypt)
'创建员工档案表
Set tb = gdb.CreateTableDef("Employee")
With tb
.Fields.Append .CreateField("EID", dbText, 4) '编号
.Fields.Append .CreateField("EName", dbText, 8) '姓名
Set idx = .CreateIndex("EID")
idx.Fields.Append .CreateField("EID")
.Indexes.Append idx
End With
gdb.TableDefs.Append tb
'释放变量对象
Set tb = Nothing
Set fld = Nothing
Set idx = Nothing
If bleFirstRun = True Then
'初次运行加入超级用户资料
' gdb.Execute ("insert into OperatorDict values('manager','manager','1111111111')")
'加入主键约束
gdb.Execute ("alter table Employee add constraint fk_eid primary key (EID)")
End If
Top
8 楼yell(我)回复于 2002-02-25 11:31:23 得分 0
我用SQL加入了主键约束,有哪位知道如何用DAO的方法加入请告知.Top




