Results 1 to 2 of 2

Thread: need help with execl and VB

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2000
    Posts
    10
    I need some help in using a excel for a database... I was wondering how to write to a an excel database. I don't have an experience with using vb and databases... I can't use Access since I don't have it on my CPU so I am confused . I tried to write to a file but that doesn't seem to work beacause I get an "invalid argument" ERROR.

    any help is appreciated.

  2. #2
    Lively Member
    Join Date
    Jun 1999
    Posts
    120
    there are some ways to open an excel file in vb...
    here's what i'd do...

    Code:
    Dim XLS As Object
    Dim XLSheet As Object
    Dim intRow As Integer
    Dim intCol As Integer
    
    ' Initialize the function, assume it's OK
        fxnExportData = True
        
    ' Set the Application (Excel)
        Set XLS = CreateObject("Excel.Application")
    '   Add a workbook
        XLS.Workbooks.Add
    '   Add a Worksheet
        XLS.Worksheets.Add
    ' Name the worksheet
        XLS.ActiveSheet.Name = "MySheetFromVB"
        Set XLSheet = XLS.Worksheets("MySheetFromVB")
    
    ' You can set the values of individual cells, e.g.
        XLSheet.Cells(1, 1) = "Company ID"
        XLSheet.Cells(1, 2) = "Employee Number"
        XLSheet.Cells(1, 3) = "Dept Code"
        XLSheet.Cells(1, 4) = "Salary"
        XLSheet.Cells(1, 5) = "Date Hired"
            
        intRow = 2
    
    ' You can also use values of controls in your forms...         XLSheet.Cells(intRow, 1) = "'" & txtCompany
       XLSheet.Cells(intRow, 2) = "'" & txtEmpNo
       XLSheet.Cells(intRow, 2) = "'" & txtDeptCode
       XLSheet.Cells(intRow, 2) = & txtSalary
       XLSheet.Cells(intRow, 3) = txtDateHired
    
    ' You can also format from VB     
    ' Format Cells/Columns/Rows
        XLSheet.Columns("E:E").NumberFormat = "dd-mm-yyyy"
        XLSheet.Rows("1:1").Font.Bold = True
        XLSheet.Columns("A:I").EntireColumn.AutoFit
    
    ' To save...
        XLSheet.SaveAs Filename:="C:\TestDir\MyXLS.xls"
        
    ' Close/Save any open Excel File and Quit the Excel Application
        XLSheet.Application.Quit
    ' Release the Excel object variables
        Set XLSheet = Nothing
        Set XLS = Nothing
    hope this helps...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width