Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Select all and copy from data grid

Posted on 2006-11-21
3
Medium Priority
?
193 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 1000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
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…
Suggested Courses

971 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