Solved

Select all and copy from data grid

Posted on 2006-11-21
3
183 Views
Last Modified: 2010-04-30
hello,

i have a data grid on a form that is populated from and sql query.

i have a number of users that would like to be able to copy the entire grid and paste it into excel is this possible as i cannot seem to select all.

also it might be nice if i could print just the grid striaght to the printer in landscape form

i am using VB6

thanks
0
Comment
Question by:Marcusw
  • 3
3 Comments
 
LVL 1

Accepted Solution

by:
RupaliV earned 250 total points
ID: 17993210
1. First clear the grid, and set the no of rows and no of cols.

Grid.Clear
Grid.Rows = No of rows
Grid.Cols = No of Cols

2. Add the data to the grid
Grid.TextMatrix(0, 0) = "One"
Grid.TextMatrix(0, 1) = "1"
Grid.TextMatrix(1, 0) = "Two"
Grid.TextMatrix(1, 1) = "2"

3. click on the button to Export the data to Excel
Private Sub Command1_Click()
    cdlgCommonDialog.CancelError = True
    cdlgCommonDialog.Flags = cdlOFNOverwritePrompt   'ask wheather to overwite if file already exists
    cdlgCommonDialog.DefaultExt = "xls"
    cdlgCommonDialog.Filter = "Text Files (*.xls)|*.xls"

    cdlgCommonDialog.ShowSave
   
    If Mid$(cdlgCommonDialog.FileName, Len(cdlgCommonDialog.FileName) - 3, 4) = ".xls" Then
        ExportDataToExcel (cdlgCommonDialog.FileName)
    Else
        ExportDataToExcel (cdlgCommonDialog.FileName & ".xls")
    End If
End Sub

Public Sub ExportDataToExcel(sFilename As String)
   
    Dim FileNum As Long
    Dim GridOfData As String
   
    With Grid
        .Row = 0
        .Col = 0
        .RowSel = .Rows - 1
        .ColSel = .Cols - 1
        GridOfData = .Clip
       
        ' Save the data to a file
        FileNum = FreeFile
        Open sFilename For Output As #FileNum
        Print #FileNum, GridOfData
        Close #FileNum
    End With
 
End Sub


4. To print the data
Private Function PrintData()
   
    Dim objApp As Object
    Dim objBook As Object
    Dim objSheet As Object
    Dim inti As Integer
    Dim intj As Integer
       
    Set objApp = CreateObject("Excel.Application") 'setting excel
    Set objBook = objApp.Workbooks.Add
    Set objSheet = objBook.Worksheets(1)
   
    objApp.Cells.Font.Name = "Courier New"
    objApp.Cells.Font.Size = 5
    objApp.Cells.Font.Bold = False
    objApp.Cells.Borders.Value = 0
    objApp.Cells.WrapText = True
   
    With objSheet
        .Range((.Cells(1, 1)), (.Cells(1, 13))).HorizontalAlignment = -4131
       
        inti = 0
        Do Until inti = Grid.Rows  'export until end of rows
            intj = 0
            Do Until intj = Grid.Cols  'get data until end of cols
                .Cells(inti + 1, intj + 1) = Trim$(Grid.TextMatrix(inti, intj))
                intj = intj + 1
            Loop
            inti = inti + 1
        Loop
    End With
   
    With objSheet.PageSetup
        .LeftMargin = objApp.InchesToPoints(0.25)
        .CenterHorizontally = False
        .CenterVertically = False
        .FitToPagesWide = 1
        .FitToPagesTall = False
        .Orientation = 2    'xlLandscape
        .PrintGridlines = True
      ' A- is the column no from where it starts printing. B-is the col no where it should stop printing;
      '1- Row no where it should start printing and Grid.Rows      is the row no where it should stop printing.
        .PrintArea = "$A$1:$B$" & Grid.Rows      
       
    End With
    objSheet.PrintOut
   
    Set objSheet = Nothing 'set to nothing and quit
    objBook.Close False
    Set objBook = Nothing
    objApp.Quit
    Set objApp = Nothing
   
    MsgBox "File Printed Successfully.", vbInformation, "File Printed..."

End Function
0
 
LVL 1

Expert Comment

by:RupaliV
ID: 18017702
Hi,

   Is the given sample code useful? Please let me know if its working.
0
 
LVL 1

Expert Comment

by:RupaliV
ID: 18290145
Hi,

     Please tell is the given sample code useful? Is it working?
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now