Vous êtes sur la page 1sur 9

Dim con As New ADODB.Connection Dim rs As New ADODB.Recordset Dim str As String Dim i As Integer con.

Open "Provider=sqloledb;Data Source=SSST41;Initial Catalog=sample;User ID=sa;Password=;" str = "select name from stutable" rs.Open str, con i=0 While Not rs.EOF Combo1.List(i) = rs.Fields(0) rs.MoveNext i=i+1 Wend rs.Close Dim con As New ADODB.Connection Dim rs As New ADODB.Recordset Dim i As Integer con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\Asm-isntcasus\database\mast.mdb;Persist Security Info=False" str = "select Name from mast" rs.Open str, con i=0 While Not rs.EOF Combo3.List(i) = rs.Fields(0) rs.MoveNext i=i+1 Wend rs.Close

Code: Dim total1 For i = 0 To MSHFlexgrid1.Rows - 1 MSHFlexgrid1.Row = i MSHFlexgrid1.Col = 4 total1 = Val(total1) + Val(MSHFlexgrid1.Text) Next
If MsgBox("Do you really want to delete this
record", vbYesNo) = vbYes Then Set cn = New ADODB.Connection sConnection = <="" span="" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 12px; backgroundcolor: transparent; background-position: initial initial; background-repeat: initial initial; "> cn.Open sConnection Set cmd.ActiveConnection = cn I = MSFlexGrid1.RowSel MSFlexGrid1.RemoveItem I End If

Private Sub HoverCommand2_Click() Dim cn As ADODB.Connection Dim sConnection As String Dim cmd As New ADODB.Command Dim strQuery As String Dim rsRecordSet As Recordset

End Sub

VB Code: Dim RowCNT As Integer, ColCNT As Integer

Dim excel_app As Object Dim excel_sheet As Object Dim max_row As Double Dim max_col As Integer Dim Excel_row As Double Dim Excel_Col As Integer Dim xl As New excel.Application Dim xlWB As New excel.Workbook Dim xlWH As New excel.Worksheet Dim i As Integer, Column As Integer, j As Integer Dim conn As ADODB.Connection Dim statement As String Dim new_value As String, new_value1 As String Dim file_path As String Dim FileName As String Dim colvalue As String, colname As String, tempvalue As String Dim Grid_row As Integer, Grid_col As Integer Dim xls As Variant If txtFilePath = "" Then MsgBox "Please first select the excel file!!" cmdBrowse.SetFocus Else file_path = txtFilePath.Text Set excel_app = CreateObject("Excel.Application") excel_app.Workbooks.Open.Text If Val(excel_app.Application.Version) >= 8 Then Set excel_sheet = excel_app.ActiveSheet Else Set excel_sheet = excel_app End If max_row = excel_sheet.UsedRange.Rows.Count max_col = excel_sheet.UsedRange.Columns.Count Screen.MousePointer = vbHourglass

Next

DoEvents flxGrd.Clear flxGrd.Rows = 2 RowCNT = 0 ColCNT = 0 'first loop for rows For i = 1 To max_row 'second loop for columns For j = 1 To max_col With flxGrd .TextMatrix(i - 1, j - 1) = _ excel_sheet.Cells(i, j).Value & " " End With Next

Screen.MousePointer = vbDefault End If

----------------------------------end of code------------------------thanks in adwance

Public Function PopulateFlexGrid(FlexGrid As Object, _ rs As Object) As Boolean '******************************************************* 'PURPOSE: Populate MSFlexGrid with data from an ' ADO Recordset

'PARAMETERS: FlexGrid: MsFlexGrid to Populate ' RS: Open ADO Recordset 'RETURNS: True if successful, false otherwise 'REQUIRES: -- Reference to Microsoft Active Data Objects ' -- Recordset should be open with cursor set at ' first row when passed and must ' support recordcount property ' -- FlexGrid should be empty when passed 'EXAMPLE: 'Dim conn As New ADODB.Connection 'Dim rs As New ADODB.Recordset 'Dim sConnString As String ' 'sConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyDatabase.mdb" 'conn.Open sConnString 'rs.Open " SELECT * FROM MyTable", oConn, adOpenKeyset, adLockOptimistic 'PopulateFlexGrid MSFlexGrid1, rs ' 'rs.Close 'conn.Close '*********************************************************** On Error GoTo ErrorHandler If Not TypeOf FlexGrid Is MSFlexGrid Then Exit Function If Not TypeOf rs Is ADODB.Recordset Then Exit Function Dim i As Integer Dim J As Integer FlexGrid.FixedRows = 1 FlexGrid.FixedCols = 0 If Not rs.EOF Then FlexGrid.Rows = rs.RecordCount + 1 FlexGrid.Cols = rs.Fields.Count

For i = 0 To rs.Fields.Count - 1 FlexGrid.TextMatrix(0, i) = rs.Fields(i).Name Next i=1

Do While Not rs.EOF For J = 0 To rs.Fields.Count - 1 If Not IsNull(rs.Fields(J).Value) Then FlexGrid.TextMatrix(i, J) = _ rs.Fields(J).Value End If Next i=i+1 rs.MoveNext Loop End If PopulateFlexGrid = True ErrorHandler: Exit Function End Function

Let's try this code.. I've been using it and it works excellent!!
1. Private Sub cmdPrint_Click() 2. 'Print the grid with the top row as a title row 3. FlexGridPrint Me.Grd, , , , , 1

4. End Sub

And also this..


1. Function FlexGridPrint(Grd As MSFlexGrid, Optional lOrientation As Long = vbPRORPortrait, Optional ByVal lMaxRowsPerPage As Long = -1, Optional lTopBorder As Long = 1000, Optional lLeftBorder As Long = 1000, Optional lRowsToRepeat As Long = 0) As Boolean 2. Dim lRowsPrinted As Long, lRowsPerPage As Long

3.

Dim lThisRow As Long, lNumRows As Long, lImageHeight As Long, lLastImageTop As Long

4.

Dim lPrinterPageHeight As Long, lPagesPrinted As Long, lHeadingHeight As Long

5. 6. 7. 8. 9. 10. 11. On Error GoTo ErrFailed Grd.TopRow = 1 lNumRows = Grd.Rows - 1 lPrinterPageHeight = Printer.Height lRowsPerPage = lMaxRowsPerPage lRowsPrinted = lRowsToRepeat

12.
13. 14. 15. 16. 17. 18. If lRowsToRepeat Then 'Calculate the height of the heading row For lThisRow = 1 To lRowsToRepeat lHeadingHeight = lHeadingHeight + Grd.RowHeight(lThisRow) Next End If

19.
20. 21. 22. 23. 24. 25. Do 'Calculate the number of rows for this page lImageHeight = 0 lRowsPerPage = 0 'Setup printer Printer.Orientation = lOrientation

26.
27. 28. 29. Then 30. 'Image has required number of rows, subtract height of current row For lThisRow = lRowsPrinted To lNumRows lImageHeight = lImageHeight + Grd.RowHeight(lThisRow) If lRowsPerPage > lMaxRowsPerPage And lMaxRowsPerPage <> -1

31. 32. 33.

lImageHeight = lImageHeight - Grd.RowHeight(lThisRow) Exit For ElseIf lImageHeight + lTopBorder * 2 + lHeadingHeight > lPrinterPageHeight Then 'Allow the same border at the bottom and top

34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45.

'Image is larger than page, subtract height of current row lImageHeight = lImageHeight - Grd.RowHeight(lThisRow) Exit For End If lRowsPerPage = lRowsPerPage + 1 Next

'Print this page lPagesPrinted = lPagesPrinted + 1 If lRowsToRepeat Then 'Print heading rows Printer.PaintPicture Grd.Picture, lLeftBorder, lTopBorder, , lHeadingHeight, , 0, , lHeadingHeight

46. 47.

'Print data rows Printer.PaintPicture Grd.Picture, lLeftBorder, lTopBorder + lHeadingHeight, , lImageHeight + lHeadingHeight, , lLastImageTop + lHeadingHeight, , lImageHeight + lHeadingHeight

48. 49. 50.

Else 'Print data rows Printer.PaintPicture Grd.Picture, lLeftBorder, lTopBorder, , lImageHeight, , lLastImageTop, , lImageHeight

51. 52. 53. 54. 55. 56.

End If

Printer.EndDoc

'Store printer position lRowsPrinted = lRowsPrinted + lRowsPerPage

57. 58. 59. 60. 61. 62. 63. 64.

lLastImageTop = lLastImageTop + lImageHeight + lHeadingHeight

Loop While lRowsPrinted < lNumRows

FlexGridPrint = True

Exit Function

65. ErrFailed: 66. 67. 68. 'Failed to print grid FlexGridPrint = False Debug.Print "Error in FlexGridPrint: " & Err.Description

69.
70. End Function

Vous aimerez peut-être aussi