vb用adodc和datagrid连接access数据库代码
1个回答
展开全部
1、在窗口创建adodc控件,datagrid控件
2、adodc的属性里,设置
a)连接数据库,可使用字符串方式(microsoft jet 3.51 ole db),access文件
b)记竖吵伍录源,cmdtext,满足条件的sql语句。
3、设置datagrid的属性:
c)datasourse设置为adodc1
4、打印可以考虑execl
菜单-》工程余或-》引用-》碰弯excel打钩
参考打印代码
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xlApp As Excel.Application
Dim strSource, strDestination As String
Dim i As Integer
Dim j As Integer
cmdPrint.Enabled = False
Screen.MousePointer = vbHourglass
Set xlApp = New Excel.Application
Set xlApp = CreateObject("Excel.Application")
'xlApp.Visible = False
strSource = App.Path & "\newtable.xls"
strDestination = App.Path & "\Temp.xls"
On Error GoTo COPYERR
FileCopy strSource, strDestination
On Error GoTo 0
Set xlBook = xlApp.Workbooks.Open(strDestination)
If bTest Then
Set xlSheet = xlBook.Worksheets(5)
Else
Set xlSheet = xlBook.Worksheets(3)
End If
If msgData.Rows > 3 Then
xlSheet.Cells(1, 1) = lblTitle
xlSheet.Cells(2, 1) = lbl1(0)
For i = 2 To Datagrid.Rows - 1
For j = 0 To Datagrid.Cols - 1
xlSheet.Cells(i + 3, j + 1) = Datagrid.TextMatrix(i, j)
Next j
Next i
xlBook.Save
If bPrint Then xlSheet.PrintOut
End If
xlApp.Quit
Screen.MousePointer = vbArrow
cmdPrint.Enabled = True
Exit Sub
COPYERR:
MsgBox Err.Description
Screen.MousePointer = vbArrow
cmdPrint.Enabled = True
2、adodc的属性里,设置
a)连接数据库,可使用字符串方式(microsoft jet 3.51 ole db),access文件
b)记竖吵伍录源,cmdtext,满足条件的sql语句。
3、设置datagrid的属性:
c)datasourse设置为adodc1
4、打印可以考虑execl
菜单-》工程余或-》引用-》碰弯excel打钩
参考打印代码
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xlApp As Excel.Application
Dim strSource, strDestination As String
Dim i As Integer
Dim j As Integer
cmdPrint.Enabled = False
Screen.MousePointer = vbHourglass
Set xlApp = New Excel.Application
Set xlApp = CreateObject("Excel.Application")
'xlApp.Visible = False
strSource = App.Path & "\newtable.xls"
strDestination = App.Path & "\Temp.xls"
On Error GoTo COPYERR
FileCopy strSource, strDestination
On Error GoTo 0
Set xlBook = xlApp.Workbooks.Open(strDestination)
If bTest Then
Set xlSheet = xlBook.Worksheets(5)
Else
Set xlSheet = xlBook.Worksheets(3)
End If
If msgData.Rows > 3 Then
xlSheet.Cells(1, 1) = lblTitle
xlSheet.Cells(2, 1) = lbl1(0)
For i = 2 To Datagrid.Rows - 1
For j = 0 To Datagrid.Cols - 1
xlSheet.Cells(i + 3, j + 1) = Datagrid.TextMatrix(i, j)
Next j
Next i
xlBook.Save
If bPrint Then xlSheet.PrintOut
End If
xlApp.Quit
Screen.MousePointer = vbArrow
cmdPrint.Enabled = True
Exit Sub
COPYERR:
MsgBox Err.Description
Screen.MousePointer = vbArrow
cmdPrint.Enabled = True
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询