Solved

Select all and copy from data grid

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

911 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

23 Experts available now in Live!

Get 1:1 Help Now