Beginner VB

Moderators: None (Apply to moderate this forum)
Number of threads: 1244
Number of posts: 2990

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
VB and Excel Posted by marl24 on 5 Aug 2008 at 12:53 AM
Hello,

I need to store the data from excel to access database table. The excel would be my data entry form and store it in an access database. Can someone please help me

thanks in advance.


Report
Re: VB and Excel Posted by dokken2 on 7 Aug 2008 at 4:58 AM
see uploaded files-
this example automates excel and uses dao to add the excel data to access

Option Explicit


'VB6 MENU - PROJECT , REFERENCES, set a reference to:
'Microsoft Excel 10.0 Object Library
'Microsoft DAO   3.60 Object Library

Const myExcel = "_Excel.xls"   'EXCEL  FILE
Const myAccess = "_Access.mdb" 'ACCESS FILE
Dim myPath As String  'PATH (IN SAME FOLDER)

Private mExcel As Excel.Application 'EXCEL


Private Sub Form_Load()
  'INIT PROJECT PATH
  myPath = App.Path + "\"
End Sub


Private Sub Command1_Click()
  ExcelToAccess
End Sub




Sub ExcelToAccess()
  On Error GoTo errHandle

  '***ACCESS DAO OBJECTS**********************************
  Dim WS As DAO.Workspace
  Dim DB As DAO.Database
  Dim RS As DAO.Recordset
  
  ' Create Microsoft Jet Workspace object
  Set WS = CreateWorkspace("ws1", "admin", "", dbUseJet)
  ' Open Database object for Microsoft Jet database
  Set DB = WS.OpenDatabase(myPath & myAccess, False)
  ' Open recordset object for write
  Set RS = DB.OpenRecordset("Table1", dbOpenDynaset)
  ' Open recordset object for read-only
  'Set rs = db.OpenRecordset("Table1", dbOpenSnapshot)
  '***ACCESS DAO OBJECTS**********************************
  
  
  '***EXCEL OBJECTS*************************************
  Set mExcel = New Excel.Application  'INIT EXCEL OBJECT
  mExcel.Visible = False 'HIDE EXCEL
 'mExcel.Visible = True  'SHOW EXCEL
  mExcel.Workbooks.Open myPath & myExcel 'OPEN XLS FILE
  
  Dim ROW As Integer    'ROW
  Dim WB1 As Object     'WORKBOOK
  Dim SH1 As Worksheet  'WORKSHEET
  
  Set WB1 = mExcel.Application.ActiveWorkbook
  Set SH1 = WB1.ActiveSheet
  '***EXCEL OBJECTS*************************************
  
  
  '*****************************************************
  'GET VALUES FROM EXCEL CELLS AND PUT INTO ACCESS TABLE
  Dim Count As Integer
  For ROW = 1 To 6
    RS.AddNew 'ADD NEW RECORD IN ACCESS
    RS!Title = SH1.Rows.Cells(ROW, 1)  'TABLE1[TITLE] = CELL[TITLE]
    RS!Year = SH1.Rows.Cells(ROW, 2)   'TABLE1[YEAR]  = CELL[YEAR]
    RS.Update 'SAVE TABLE1 RECORD IN ACCESS
    Count = Count + 1
  Next  'NEXT CELL
  '*****************************************************
  
xit:
  'CLOSE ACCESS-DAO OBJECTS
  RS.Close
  DB.Close
  WS.Close
xit2:
  'RELEASE ACCESS-DAO OBJECTS
  Set RS = Nothing
  Set DB = Nothing
  Set DB = Nothing
    
  'QUIT EXCEL
  mExcel.Quit
  Set mExcel = Nothing
  
  MsgBox "Records inserted:" & Count, vbInformation, "Done!"
Exit Sub


errHandle:
    If Err.Number = 91 Then Resume xit2
    MsgBox Err.Description, vbCritical, Err.Number
    Resume xit
End Sub
Attachment: Automation.zip (17198 Bytes | downloaded 203 times)
Report
Re: VB and Excel Posted by mOski on 13 Aug 2008 at 8:33 PM
: see uploaded files-
: this example automates excel and uses dao to add the excel data to
: access
:
:
: 
: Option Explicit
: 
: 
: 'VB6 MENU - PROJECT , REFERENCES, set a reference to:
: 'Microsoft Excel 10.0 Object Library
: 'Microsoft DAO   3.60 Object Library
: 
: Const myExcel = "_Excel.xls"   'EXCEL  FILE
: Const myAccess = "_Access.mdb" 'ACCESS FILE
: Dim myPath As String  'PATH (IN SAME FOLDER)
: 
: Private mExcel As Excel.Application 'EXCEL
: 
: 
: Private Sub Form_Load()
:   'INIT PROJECT PATH
:   myPath = App.Path + "\"
: End Sub
: 
: 
: Private Sub Command1_Click()
:   ExcelToAccess
: End Sub
: 
: 
: 
: 
: Sub ExcelToAccess()
:   On Error GoTo errHandle
: 
:   '***ACCESS DAO OBJECTS**********************************
:   Dim WS As DAO.Workspace
:   Dim DB As DAO.Database
:   Dim RS As DAO.Recordset
:   
:   ' Create Microsoft Jet Workspace object
:   Set WS = CreateWorkspace("ws1", "admin", "", dbUseJet)
:   ' Open Database object for Microsoft Jet database
:   Set DB = WS.OpenDatabase(myPath & myAccess, False)
:   ' Open recordset object for write
:   Set RS = DB.OpenRecordset("Table1", dbOpenDynaset)
:   ' Open recordset object for read-only
:   'Set rs = db.OpenRecordset("Table1", dbOpenSnapshot)
:   '***ACCESS DAO OBJECTS**********************************
:   
:   
:   '***EXCEL OBJECTS*************************************
:   Set mExcel = New Excel.Application  'INIT EXCEL OBJECT
:   mExcel.Visible = False 'HIDE EXCEL
:  'mExcel.Visible = True  'SHOW EXCEL
:   mExcel.Workbooks.Open myPath & myExcel 'OPEN XLS FILE
:   
:   Dim ROW As Integer    'ROW
:   Dim WB1 As Object     'WORKBOOK
:   Dim SH1 As Worksheet  'WORKSHEET
:   
:   Set WB1 = mExcel.Application.ActiveWorkbook
:   Set SH1 = WB1.ActiveSheet
:   '***EXCEL OBJECTS*************************************
:   
:   
:   '*****************************************************
:   'GET VALUES FROM EXCEL CELLS AND PUT INTO ACCESS TABLE
:   Dim Count As Integer
:   For ROW = 1 To 6
:     RS.AddNew 'ADD NEW RECORD IN ACCESS
:     RS!Title = SH1.Rows.Cells(ROW, 1)  'TABLE1[TITLE] = CELL[TITLE]
:     RS!Year = SH1.Rows.Cells(ROW, 2)   'TABLE1[YEAR]  = CELL[YEAR]
:     RS.Update 'SAVE TABLE1 RECORD IN ACCESS
:     Count = Count + 1
:   Next  'NEXT CELL
:   '*****************************************************
:   
: xit:
:   'CLOSE ACCESS-DAO OBJECTS
:   RS.Close
:   DB.Close
:   WS.Close
: xit2:
:   'RELEASE ACCESS-DAO OBJECTS
:   Set RS = Nothing
:   Set DB = Nothing
:   Set DB = Nothing
:     
:   'QUIT EXCEL
:   mExcel.Quit
:   Set mExcel = Nothing
:   
:   MsgBox "Records inserted:" & Count, vbInformation, "Done!"
: Exit Sub
: 
: 
: errHandle:
:     If Err.Number = 91 Then Resume xit2
:     MsgBox Err.Description, vbCritical, Err.Number
:     Resume xit
: End Sub
: 
:



I just tried the code and works! thank you soOoOoOo muCh for the code!

(^_^)


Report
Re: VB and Excel Posted by marl24 on 28 Aug 2008 at 1:03 AM
Thank you very much..

: : see uploaded files-
: : this example automates excel and uses dao to add the excel data to
: : access
: :
: :
: : 
: : Option Explicit
: : 
: : 
: : 'VB6 MENU - PROJECT , REFERENCES, set a reference to:
: : 'Microsoft Excel 10.0 Object Library
: : 'Microsoft DAO   3.60 Object Library
: : 
: : Const myExcel = "_Excel.xls"   'EXCEL  FILE
: : Const myAccess = "_Access.mdb" 'ACCESS FILE
: : Dim myPath As String  'PATH (IN SAME FOLDER)
: : 
: : Private mExcel As Excel.Application 'EXCEL
: : 
: : 
: : Private Sub Form_Load()
: :   'INIT PROJECT PATH
: :   myPath = App.Path + "\"
: : End Sub
: : 
: : 
: : Private Sub Command1_Click()
: :   ExcelToAccess
: : End Sub
: : 
: : 
: : 
: : 
: : Sub ExcelToAccess()
: :   On Error GoTo errHandle
: : 
: :   '***ACCESS DAO OBJECTS**********************************
: :   Dim WS As DAO.Workspace
: :   Dim DB As DAO.Database
: :   Dim RS As DAO.Recordset
: :   
: :   ' Create Microsoft Jet Workspace object
: :   Set WS = CreateWorkspace("ws1", "admin", "", dbUseJet)
: :   ' Open Database object for Microsoft Jet database
: :   Set DB = WS.OpenDatabase(myPath & myAccess, False)
: :   ' Open recordset object for write
: :   Set RS = DB.OpenRecordset("Table1", dbOpenDynaset)
: :   ' Open recordset object for read-only
: :   'Set rs = db.OpenRecordset("Table1", dbOpenSnapshot)
: :   '***ACCESS DAO OBJECTS**********************************
: :   
: :   
: :   '***EXCEL OBJECTS*************************************
: :   Set mExcel = New Excel.Application  'INIT EXCEL OBJECT
: :   mExcel.Visible = False 'HIDE EXCEL
: :  'mExcel.Visible = True  'SHOW EXCEL
: :   mExcel.Workbooks.Open myPath & myExcel 'OPEN XLS FILE
: :   
: :   Dim ROW As Integer    'ROW
: :   Dim WB1 As Object     'WORKBOOK
: :   Dim SH1 As Worksheet  'WORKSHEET
: :   
: :   Set WB1 = mExcel.Application.ActiveWorkbook
: :   Set SH1 = WB1.ActiveSheet
: :   '***EXCEL OBJECTS*************************************
: :   
: :   
: :   '*****************************************************
: :   'GET VALUES FROM EXCEL CELLS AND PUT INTO ACCESS TABLE
: :   Dim Count As Integer
: :   For ROW = 1 To 6
: :     RS.AddNew 'ADD NEW RECORD IN ACCESS
: :     RS!Title = SH1.Rows.Cells(ROW, 1)  'TABLE1[TITLE] = CELL[TITLE]
: :     RS!Year = SH1.Rows.Cells(ROW, 2)   'TABLE1[YEAR]  = CELL[YEAR]
: :     RS.Update 'SAVE TABLE1 RECORD IN ACCESS
: :     Count = Count + 1
: :   Next  'NEXT CELL
: :   '*****************************************************
: :   
: : xit:
: :   'CLOSE ACCESS-DAO OBJECTS
: :   RS.Close
: :   DB.Close
: :   WS.Close
: : xit2:
: :   'RELEASE ACCESS-DAO OBJECTS
: :   Set RS = Nothing
: :   Set DB = Nothing
: :   Set DB = Nothing
: :     
: :   'QUIT EXCEL
: :   mExcel.Quit
: :   Set mExcel = Nothing
: :   
: :   MsgBox "Records inserted:" & Count, vbInformation, "Done!"
: : Exit Sub
: : 
: : 
: : errHandle:
: :     If Err.Number = 91 Then Resume xit2
: :     MsgBox Err.Description, vbCritical, Err.Number
: :     Resume xit
: : End Sub
: : 
: :
:
:
:
: I just tried the code and works! thank you soOoOoOo muCh for the
: code!
:
: (^_^)
:
:
:



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.