源码如下Option ExplicitPrivate Sub Form_Load() ' End Sub Private Sub cmdShow_Click() Dim i As Integer '普通循环变量 Dim sngWidth As Single '控件的宽度 Dim sngHeight As Single '控件的高度 Dim ctlAnyControl As Control 'sngWidth = 1000 '控件的宽度取窗口宽度的 1/3 'sngHeight = (frmShow.Height - 400) / CInt(txtNumberOfButtons) '控件的高度取窗口高度的 1/控件个数 For i = 1 To CInt(txtNumberOfButtons) '在frmShow上添加按钮 Call frmShow.Controls.Add("VB.CommandButton", "Command" & CStr(i)) '用序号引用Controls集合中的按钮 Set ctlAnyControl = frmShow.Controls.Item(i - 1) '设置按钮的属性 With ctlAnyControl .Top = (i - 1) * CInt(txtheithofcmdbutton) .Left = CInt(txtleftdistanceofcmdbutton) .Width = CInt(txtwidthofcmdbutton) .Height = CInt(txtheithofcmdbutton) .Caption = "按钮" & CStr(i) .Visible = True .DragMode = 1 End With Next i '注意这儿的Index属性,它指示了触发事件的控件在控件数组中的下标 'sngHeight = (frmShow.Height - 400) / CInt(txtNumberOfTextboxes) '控件的高度取窗口高度的 1/控件个数 For i = 1 To CInt(txtNumberOfTextboxes) '在frmShow上添加文本框 Call frmShow.Controls.Add("VB.Textbox", "Text" & CStr(i)) '用名字引用加Controls集合中的文本框 Set ctlAnyControl = frmShow.Controls.Item("Text" & CStr(i)) '设置文本框的属性 With ctlAnyControl .Top = (i - 1) * CInt(txtheithoftextbox) .Left = CInt(txtleftdistanceoftextbox) .Width = CInt(txtwidthoftextbox) .Height = CInt(txtheithoftextbox) .Visible = True .Text = "文本框" & CStr(i) .Visible = True .DragMode = 1 End With Next i 'sngHeight = (frmShow.Height - 400) / CInt(txtNumberOfLabels) '控件的高度取窗口高度的 1/控件个数 For i = 1 To CInt(txtNumberOfLabels) '在frmShow上添加标签 Call frmShow.Controls.Add("VB.Label", "Label" & CStr(i)) '用名字引用加Controls集合中的标签 Set ctlAnyControl = frmShow.Controls.Item("Label" & CStr(i)) '设置标签的属性 With ctlAnyControl .Top = (i - 1) * CInt(txtheithoflabel) .Left = CInt(txtdistanceoflabel) .Width = CInt(txtwidthoflabel) .Height = CInt(txtheithoflabel) .Visible = True .Caption = "标签" & CStr(i) .Visible = True .DragMode = 1 End With Next i
Option Explicit Private conn1 As ADODB.Connection Private rs1 As ADODB.RecordsetPrivate Sub Command2_Click() Dim dbfilename As String Dim strsql As String Dim ConnectString As String Dim i As Integer Set conn1 = New ADODB.Connection dbfilename = "F:\csdn_vb\database\listbox\article1.mdb" ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbfilename & ";Persist Security Info=False;" conn1.Open ConnectStringSet rs1 = New ADODB.Recordsetstrsql = "select * from mz" rs1.Open strsql, conn1, 1, 3rs1.AddNew rs1.Fields("mc") = Text1.Text rs1.Fields("memo") = Text2.Text rs1.UpdateEnd Sub
online(龙卷风V2.0--再战江湖) 大哥已经说得很清楚了啊! Option Explicit Private conn1 As ADODB.Connection ‘建立与数据库连接得源 Private rs1 As ADODB.RecordsetPrivate Sub Command2_Click() Dim dbfilename As String Dim strsql As String Dim ConnectString As String Dim i As Integer Set conn1 = New ADODB.Connection '实例化ADODB.Connection 对象 dbfilename = "F:\csdn_vb\database\listbox\article1.mdb" ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbfilename & ";Persist Security Info=False;"'ado数据源的连接字符串 conn1.Open ConnectStringSet rs1 = New ADODB.Recordsetstrsql = "select * from mz" rs1.Open strsql, conn1, 1, 3'数据集的打开rs1.AddNew rs1.Fields("mc") = Text1.Text’保存 rs1.Fields("memo") = Text2.Text rs1.UpdateEnd Sub
Option Explicit'工程->引用->Microsoft ActiveX Data Objects 2.0 Library (后面为版本号) Dim cn As New ADODB.Connection Dim rs As New ADODB.RecordsetPrivate Sub Command1_Click() rs.Open "SELECT * FROM TABLENAME WHERE NAME = '" & txtName.Text & "'", cn, adOpenDynamic, adLockOptimistic If Not rs.EOF Then Label1.Caption = rs!ID Label2.Caption = rs!Memo End If
rs.AddNew rs!ID = "编号" rs!Memo = "备注" rs.Update End SubPrivate Sub Form_Load() cn.ConnectionString = "DBQ=" & App.Path & "\TelePhone.mdb;DefaultDir=" & _ App.Path & ";Driver={Microsoft Access Driver (*.mdb)};" & _ "DriverId=25;FIL=MS Access;ImplicitCommitSync=Yes;" & _ "MaxBufferSize=512;MaxScanRows=8;PageTimeout=5;SafeTransactions=0;" & _ "Threads=3;UID=ADMIN;UserCommitSync=Yes;PWD=admind1234;" cn.Open '查询字符串可以上这里查 'http://www.connectionstrings.com/ End SubPrivate Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) On Error Resume Next rs.Close Set rs = Nothing cn.Close Set cn = Nothing End Sub
Option Explicit'工程->引用->Microsoft ActiveX Data Objects 2.0 Library (后面为版本号) Dim cn As New ADODB.Connection Dim rs As New ADODB.Recordset private sub form_load() cn.ConnectionString = "driver={SQL Server};server=yang;uid=sa;pwd=;database=zjy" cn.open '//打开数据源 cnn.CursorLocation = adUseClient end sub private sub commamd1_click() on error goto err: cnn.BeginTrans cn.execute("insert into 表(字段) vlaues('" & trim(text1) & "')") cnn.CommitTrans exit sub err: cnn.RollbackTrans msgbox err.Description end sub
Public txtSQL As String Public admin As Boolean Public Function ExecuteSQL(ByVal sql As String) As ADODB.Recordset Dim mycon As ADODB.Connection Dim rst As ADODB.Recordset Set mycon = New ADODB.Connection Set rst = New ADODB.Recordset mycon.ConnectionString() = connstring() mycon.Open rst.Open Trim(sql), mycon, adOpenKeyset, adLockOptimistic Set ExecuteSQL = rst End FunctionPublic Function connstring() As String connstring = "FileDSN=mycon.dsn;uid=sa;pwd=" End Function這樣就哦了!
'
End Sub
Private Sub cmdShow_Click()
Dim i As Integer '普通循环变量
Dim sngWidth As Single '控件的宽度
Dim sngHeight As Single '控件的高度
Dim ctlAnyControl As Control
'sngWidth = 1000 '控件的宽度取窗口宽度的 1/3
'sngHeight = (frmShow.Height - 400) / CInt(txtNumberOfButtons) '控件的高度取窗口高度的 1/控件个数
For i = 1 To CInt(txtNumberOfButtons)
'在frmShow上添加按钮
Call frmShow.Controls.Add("VB.CommandButton", "Command" & CStr(i))
'用序号引用Controls集合中的按钮
Set ctlAnyControl = frmShow.Controls.Item(i - 1)
'设置按钮的属性
With ctlAnyControl
.Top = (i - 1) * CInt(txtheithofcmdbutton)
.Left = CInt(txtleftdistanceofcmdbutton)
.Width = CInt(txtwidthofcmdbutton)
.Height = CInt(txtheithofcmdbutton)
.Caption = "按钮" & CStr(i)
.Visible = True
.DragMode = 1
End With
Next i
'注意这儿的Index属性,它指示了触发事件的控件在控件数组中的下标 'sngHeight = (frmShow.Height - 400) / CInt(txtNumberOfTextboxes) '控件的高度取窗口高度的 1/控件个数
For i = 1 To CInt(txtNumberOfTextboxes)
'在frmShow上添加文本框
Call frmShow.Controls.Add("VB.Textbox", "Text" & CStr(i))
'用名字引用加Controls集合中的文本框
Set ctlAnyControl = frmShow.Controls.Item("Text" & CStr(i))
'设置文本框的属性
With ctlAnyControl
.Top = (i - 1) * CInt(txtheithoftextbox)
.Left = CInt(txtleftdistanceoftextbox)
.Width = CInt(txtwidthoftextbox)
.Height = CInt(txtheithoftextbox)
.Visible = True
.Text = "文本框" & CStr(i)
.Visible = True
.DragMode = 1
End With
Next i
'sngHeight = (frmShow.Height - 400) / CInt(txtNumberOfLabels) '控件的高度取窗口高度的 1/控件个数
For i = 1 To CInt(txtNumberOfLabels)
'在frmShow上添加标签
Call frmShow.Controls.Add("VB.Label", "Label" & CStr(i))
'用名字引用加Controls集合中的标签
Set ctlAnyControl = frmShow.Controls.Item("Label" & CStr(i))
'设置标签的属性
With ctlAnyControl
.Top = (i - 1) * CInt(txtheithoflabel)
.Left = CInt(txtdistanceoflabel)
.Width = CInt(txtwidthoflabel)
.Height = CInt(txtheithoflabel)
.Visible = True
.Caption = "标签" & CStr(i)
.Visible = True
.DragMode = 1
End With
Next i
'打开展示窗口
frmShow.Show vbModal, frmMain
End Sub
Adodc1.CommandType = adCmdText
Adodc1.RecordSource = "Select * from 表"
Adodc1.Refresh
Private conn1 As ADODB.Connection
Private rs1 As ADODB.RecordsetPrivate Sub Command2_Click()
Dim dbfilename As String
Dim strsql As String
Dim ConnectString As String
Dim i As Integer
Set conn1 = New ADODB.Connection
dbfilename = "F:\csdn_vb\database\listbox\article1.mdb"
ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbfilename & ";Persist Security Info=False;"
conn1.Open ConnectStringSet rs1 = New ADODB.Recordsetstrsql = "select * from mz"
rs1.Open strsql, conn1, 1, 3rs1.AddNew
rs1.Fields("mc") = Text1.Text
rs1.Fields("memo") = Text2.Text
rs1.UpdateEnd Sub
ConnectionString = "driver={SQL Server};server=yang;uid=sa;pwd=;ConnectionTimeout=0;database=zjy"
Option Explicit
Private conn1 As ADODB.Connection ‘建立与数据库连接得源
Private rs1 As ADODB.RecordsetPrivate Sub Command2_Click()
Dim dbfilename As String
Dim strsql As String
Dim ConnectString As String
Dim i As Integer
Set conn1 = New ADODB.Connection '实例化ADODB.Connection 对象
dbfilename = "F:\csdn_vb\database\listbox\article1.mdb"
ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbfilename & ";Persist Security Info=False;"'ado数据源的连接字符串
conn1.Open ConnectStringSet rs1 = New ADODB.Recordsetstrsql = "select * from mz"
rs1.Open strsql, conn1, 1, 3'数据集的打开rs1.AddNew
rs1.Fields("mc") = Text1.Text’保存
rs1.Fields("memo") = Text2.Text
rs1.UpdateEnd Sub
Dim cn As New ADODB.Connection
Dim rs As New ADODB.RecordsetPrivate Sub Command1_Click()
rs.Open "SELECT * FROM TABLENAME WHERE NAME = '" & txtName.Text & "'", cn, adOpenDynamic, adLockOptimistic
If Not rs.EOF Then
Label1.Caption = rs!ID
Label2.Caption = rs!Memo
End If
rs.AddNew
rs!ID = "编号"
rs!Memo = "备注"
rs.Update
End SubPrivate Sub Form_Load()
cn.ConnectionString = "DBQ=" & App.Path & "\TelePhone.mdb;DefaultDir=" & _
App.Path & ";Driver={Microsoft Access Driver (*.mdb)};" & _
"DriverId=25;FIL=MS Access;ImplicitCommitSync=Yes;" & _
"MaxBufferSize=512;MaxScanRows=8;PageTimeout=5;SafeTransactions=0;" & _
"Threads=3;UID=ADMIN;UserCommitSync=Yes;PWD=admind1234;"
cn.Open
'查询字符串可以上这里查
'http://www.connectionstrings.com/
End SubPrivate Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
On Error Resume Next
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
private sub form_load()
cn.ConnectionString = "driver={SQL Server};server=yang;uid=sa;pwd=;database=zjy"
cn.open '//打开数据源
cnn.CursorLocation = adUseClient
end sub
private sub commamd1_click()
on error goto err:
cnn.BeginTrans
cn.execute("insert into 表(字段) vlaues('" & trim(text1) & "')")
cnn.CommitTrans
exit sub
err:
cnn.RollbackTrans
msgbox err.Description
end sub
Public admin As Boolean
Public Function ExecuteSQL(ByVal sql As String) As ADODB.Recordset
Dim mycon As ADODB.Connection
Dim rst As ADODB.Recordset
Set mycon = New ADODB.Connection
Set rst = New ADODB.Recordset
mycon.ConnectionString() = connstring()
mycon.Open
rst.Open Trim(sql), mycon, adOpenKeyset, adLockOptimistic
Set ExecuteSQL = rst
End FunctionPublic Function connstring() As String
connstring = "FileDSN=mycon.dsn;uid=sa;pwd="
End Function這樣就哦了!