Click here to Skip to main content
15,892,005 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: Adding Entries of Two columns in SQL database using VBasic. Pin
Johan Hakkesteegt12-Nov-09 1:04
Johan Hakkesteegt12-Nov-09 1:04 
QuestionSorting Excel range of cells by column in VB Pin
nhsal6911-Nov-09 22:50
nhsal6911-Nov-09 22:50 
AnswerRe: Sorting Excel range of cells by column in VB Pin
Johan Hakkesteegt11-Nov-09 23:41
Johan Hakkesteegt11-Nov-09 23:41 
GeneralRe: Sorting Excel range of cells by column in VB Pin
nhsal6912-Nov-09 0:20
nhsal6912-Nov-09 0:20 
GeneralRe: Sorting Excel range of cells by column in VB [modified] Pin
Johan Hakkesteegt12-Nov-09 0:26
Johan Hakkesteegt12-Nov-09 0:26 
GeneralRe: Sorting Excel range of cells by column in VB Pin
nhsal6912-Nov-09 1:05
nhsal6912-Nov-09 1:05 
GeneralRe: Sorting Excel range of cells by column in VB Pin
Johan Hakkesteegt12-Nov-09 2:03
Johan Hakkesteegt12-Nov-09 2:03 
GeneralRe: Sorting Excel range of cells by column in VB Pin
Johan Hakkesteegt12-Nov-09 3:16
Johan Hakkesteegt12-Nov-09 3:16 
I got this to work for me (Excel 2007):

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim xlApp As Excel.Application
        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet
        Dim misValue As Object = System.Reflection.Missing.Value

        Dim i As Integer = 1


        Dim tmpConn As New SqlConnection(connStrDBHERFI)
        Dim tmpComm As SqlCommand = Nothing
        Dim tmpRdr As SqlDataReader = Nothing
        Try

            xlApp = New Excel.ApplicationClass
            xlWorkBook = xlApp.Workbooks.Add(misValue)
            xlWorkSheet = xlWorkBook.Sheets("sheet1")
            Me.Cursor = Cursors.WaitCursor
            If Not tmpConn.State = ConnectionState.Open Then tmpConn.Open()
            tmpComm = New SqlCommand("Select cardcode, cardname from ocrd where 'y' not in (FrozenFor, ValidFor)", tmpConn)
            tmpRdr = tmpComm.ExecuteReader(CommandBehavior.Default)
            If tmpRdr.HasRows Then
                Do While tmpRdr.Read
                    xlWorkSheet.Cells(i, 1) = tmpRdr.Item("CardCode")
                    xlWorkSheet.Cells(i, 2) = tmpRdr.Item("CardName")
                    i += 1
                Loop
                With xlWorkSheet.Range("A1", "B" & i)
                    '.BorderAround()'
                    .Sort(Key1:=.Columns(2), Order1:=Excel.XlSortOrder.xlAscending, _
    Key2:=.Columns(1), Order2:=Excel.XlSortOrder.xlAscending, _
    Orientation:=Excel.XlSortOrientation.xlSortColumns, _
    Header:=Excel.XlYesNoGuess.xlNo, _
    SortMethod:=Excel.XlSortMethod.xlPinYin, _
    DataOption1:=Excel.XlSortDataOption.xlSortNormal, _
    DataOption2:=Excel.XlSortDataOption.xlSortNormal, _
    DataOption3:=Excel.XlSortDataOption.xlSortNormal)
                End With


            End If
            tmpRdr.Close()
            tmpConn.Close()
            tmpConn.Dispose()

            xlWorkSheet.SaveAs("c:\Temp\test.xlsx")
            xlWorkBook.Close()
            xlApp.Quit()

            releaseObject(xlApp)
            releaseObject(xlWorkBook)
            releaseObject(xlWorkSheet)

        Catch ex As Exception
            MsgBox(ex.ToString)
        Finally
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub releaseObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try
    End Sub


My advice is free, and you may get what you paid for.

GeneralRe: Sorting Excel range of cells by column in VB Pin
Oakman12-Nov-09 16:03
Oakman12-Nov-09 16:03 
GeneralRe: Sorting Excel range of cells by column in VB Pin
Johan Hakkesteegt12-Nov-09 20:18
Johan Hakkesteegt12-Nov-09 20:18 
GeneralRe: Sorting Excel range of cells by column in VB Pin
nhsal6913-Nov-09 0:08
nhsal6913-Nov-09 0:08 
GeneralRe: Sorting Excel range of cells by column in VB Pin
Johan Hakkesteegt13-Nov-09 3:14
Johan Hakkesteegt13-Nov-09 3:14 
QuestionChange Process Name Pin
Anubhava Dimri11-Nov-09 18:49
Anubhava Dimri11-Nov-09 18:49 
AnswerRe: Change Process Name Pin
The Man from U.N.C.L.E.11-Nov-09 22:40
The Man from U.N.C.L.E.11-Nov-09 22:40 
QuestionVB.NET Audio Help Pin
smccloud11-Nov-09 16:34
smccloud11-Nov-09 16:34 
AnswerRe: VB.NET Audio Help Pin
Eduard Keilholz11-Nov-09 23:35
Eduard Keilholz11-Nov-09 23:35 
GeneralRe: VB.NET Audio Help Pin
smccloud12-Nov-09 2:56
smccloud12-Nov-09 2:56 
GeneralRe: VB.NET Audio Help Pin
Eduard Keilholz12-Nov-09 4:56
Eduard Keilholz12-Nov-09 4:56 
QuestionFile read and write as a stream Pin
Praveen Thankappan11-Nov-09 8:13
Praveen Thankappan11-Nov-09 8:13 
AnswerRe: File read and write as a stream Pin
EliottA11-Nov-09 8:42
EliottA11-Nov-09 8:42 
AnswerRe: File read and write as a stream Pin
Christian Graus11-Nov-09 9:44
protectorChristian Graus11-Nov-09 9:44 
GeneralRe: File read and write as a stream Pin
Smithers-Jones12-Nov-09 2:46
Smithers-Jones12-Nov-09 2:46 
GeneralRe: File read and write as a stream Pin
Praveen Thankappan12-Nov-09 7:02
Praveen Thankappan12-Nov-09 7:02 
QuestionAccess level and user right Pin
waner michaud11-Nov-09 7:20
waner michaud11-Nov-09 7:20 
AnswerRe: Access level and user right Pin
Dave Kreskowiak11-Nov-09 7:32
mveDave Kreskowiak11-Nov-09 7:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.