vb读写xls、数据库文件
引用ADO Library对xls文件进行修改。
可以在xls文件打开的情况下直接操作,并在Excel或Wps中直接更新。
Private Sub Command1_Click() On Error Resume Next Dim adoConn As New ADODB.Connection Dim adoReco As New ADODB.Recordset Set adoConn = New ADODB.Connection adoConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=D:/1.xls;Extended Properties='Excel 8.0;HDR=Yes'" Sql = "Num int, Name char(20)" adoConn.Execute "Create table [D:/1.xls].Sheet1" & "(" & Sql & ")" adoConn.Execute "insert into [sheet1$] values (999,'zichu')" adoConn.Execute "insert into [sheet1$] values (888,'jingyi')" adoConn.Execute "UPDATE [sheet1$] Set Name='JingYi' Where Num=888" adoConn.Close Set adoConn = Nothing End Sub
vb操作数据库读取基本用法
Dim cnn As New ADODB.Connection Dim rs As New ADODB.Recordset Private Sub Command1_Click() Dim str As String str = App.Path str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & str & "\aa.mdb;Mode=Read;Persist Security Info=False" cnn.Open str rs.Open "select * from A表 where cint(A100)>2", cnn, adOpenKeyset, adLockPessimistic End Sub Private Sub Command2_Click() If rs.EOF Then Exit Sub Text1.Text = rs.Fields("SID") Text2.Text = rs.Fields("A100") Text3.Text = rs.Fields("A101") rs.MoveNext End Sub Private Sub Command3_Click() rs.Close cnn.Close End Sub