Solved

Select all and copy from data grid

Posted on 2006-11-21
3
186 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

840 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