home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 5_2007-2008.ISO / data / Zips / Excel_to_F2049032202007.psc / Excel_to_Grid_to_Access / Form1.frm < prev    next >
Text File  |  2007-02-12  |  8KB  |  286 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  3. Object = "{5E9E78A0-531B-11CF-91F6-C2863C385E30}#1.0#0"; "MSFLXGRD.OCX"
  4. Begin VB.Form Form1 
  5.    Caption         =   "Excel to Grid to Access (Database)"
  6.    ClientHeight    =   5565
  7.    ClientLeft      =   60
  8.    ClientTop       =   450
  9.    ClientWidth     =   6990
  10.    LinkTopic       =   "Form1"
  11.    MaxButton       =   0   'False
  12.    ScaleHeight     =   5565
  13.    ScaleWidth      =   6990
  14.    StartUpPosition =   3  'Windows Default
  15.    Begin VB.Data Data1 
  16.       Caption         =   "Data1"
  17.       Connect         =   "Access"
  18.       DatabaseName    =   ""
  19.       DefaultCursorType=   0  'DefaultCursor
  20.       DefaultType     =   2  'UseODBC
  21.       Exclusive       =   0   'False
  22.       Height          =   345
  23.       Left            =   600
  24.       Options         =   0
  25.       ReadOnly        =   0   'False
  26.       RecordsetType   =   1  'Dynaset
  27.       RecordSource    =   ""
  28.       Top             =   6120
  29.       Width           =   1140
  30.    End
  31.    Begin VB.TextBox Textfile 
  32.       BeginProperty Font 
  33.          Name            =   "MS Sans Serif"
  34.          Size            =   12
  35.          Charset         =   0
  36.          Weight          =   700
  37.          Underline       =   0   'False
  38.          Italic          =   0   'False
  39.          Strikethrough   =   0   'False
  40.       EndProperty
  41.       Height          =   495
  42.       Left            =   1680
  43.       TabIndex        =   5
  44.       Top             =   120
  45.       Width           =   5175
  46.    End
  47.    Begin VB.ComboBox Combo1 
  48.       Height          =   315
  49.       Left            =   2520
  50.       TabIndex        =   3
  51.       Top             =   4920
  52.       Width           =   1215
  53.    End
  54.    Begin MSComDlg.CommonDialog CommonDialog 
  55.       Left            =   2040
  56.       Top             =   6000
  57.       _ExtentX        =   847
  58.       _ExtentY        =   847
  59.       _Version        =   393216
  60.    End
  61.    Begin MSFlexGridLib.MSFlexGrid MSFlexGrid 
  62.       Height          =   3855
  63.       Left            =   120
  64.       TabIndex        =   2
  65.       Top             =   960
  66.       Width           =   6735
  67.       _ExtentX        =   11880
  68.       _ExtentY        =   6800
  69.       _Version        =   393216
  70.       Cols            =   0
  71.       FixedCols       =   0
  72.    End
  73.    Begin VB.CommandButton Save_to_database 
  74.       Caption         =   "SAVE TO DATABASE"
  75.       Enabled         =   0   'False
  76.       Height          =   495
  77.       Left            =   4680
  78.       TabIndex        =   1
  79.       Top             =   4920
  80.       Width           =   2055
  81.    End
  82.    Begin VB.CommandButton Load_excel 
  83.       Caption         =   "LOAD EXCEL"
  84.       Height          =   495
  85.       Left            =   120
  86.       TabIndex        =   0
  87.       Top             =   120
  88.       Width           =   1455
  89.    End
  90.    Begin VB.Label Label1 
  91.       Caption         =   "Select Worksheet :"
  92.       BeginProperty Font 
  93.          Name            =   "Trebuchet MS"
  94.          Size            =   12
  95.          Charset         =   0
  96.          Weight          =   700
  97.          Underline       =   0   'False
  98.          Italic          =   0   'False
  99.          Strikethrough   =   0   'False
  100.       EndProperty
  101.       Height          =   375
  102.       Left            =   240
  103.       TabIndex        =   6
  104.       Top             =   4920
  105.       Width           =   2175
  106.    End
  107.    Begin VB.Label Label2 
  108.       Caption         =   "WorkSheet:"
  109.       BeginProperty Font 
  110.          Name            =   "Trebuchet MS"
  111.          Size            =   12
  112.          Charset         =   0
  113.          Weight          =   700
  114.          Underline       =   0   'False
  115.          Italic          =   0   'False
  116.          Strikethrough   =   0   'False
  117.       EndProperty
  118.       Height          =   375
  119.       Left            =   240
  120.       TabIndex        =   4
  121.       Top             =   3840
  122.       Width           =   1335
  123.    End
  124. End
  125. Attribute VB_Name = "Form1"
  126. Attribute VB_GlobalNameSpace = False
  127. Attribute VB_Creatable = False
  128. Attribute VB_PredeclaredId = True
  129. Attribute VB_Exposed = False
  130.  
  131. '# Author: Arthur S. Molu±as   #'
  132. '
  133. '# Sa±ulom Media Corp.         #'
  134. '
  135. '# E-Mail : dsaulom@yahoo.com  #'
  136.  
  137.  
  138. '#  Note:
  139. '       To view the transfered data
  140. '       open/check the database
  141. '       The loading of excel to grid may take
  142. '       a second so be patient.
  143. '
  144. '   Have Fun!
  145.  
  146. Dim XLS As Excel.Application
  147.  
  148. Dim WBOOK As Excel.Workbook
  149.  
  150. Dim WSHEET As Excel.Worksheet
  151.  
  152. Dim RNG As Excel.Range
  153.  
  154. Dim R As Integer
  155.  
  156. Dim C As Integer
  157.  
  158. Dim COUNTER As Integer
  159.  
  160. Dim cntfileds As Integer
  161.  
  162. Dim WSheetno As Integer
  163.  
  164. Dim db As Database
  165.  
  166. Dim rs As Recordset
  167.  
  168.  
  169. Private Sub Combo1_Click()
  170. On Error Resume Next:
  171.     Call load_to_grid
  172.     Save_to_database.Enabled = True
  173. End Sub
  174.  
  175. Private Sub Load_excel_Click()
  176. On Error Resume Next
  177. 'browse xls file
  178. With CommonDialog
  179.     'Set title
  180.     .DialogTitle = "Open Excel Files"
  181.     'Set filename to Null
  182.     .filename = ""
  183.      'Select a filter
  184.     .Filter = "Excel Files (*.xls)" + Chr$(124) + "*.xls" + Chr$(124)
  185.     .ShowOpen
  186. End With
  187. 'load filename to text1
  188. Textfile.Text = CommonDialog.filename
  189. 'Create a new instance of Excel
  190. Set XLS = CreateObject("Excel.Application")
  191. 'Open XLS file
  192. Set WBOOK = XLS.Workbooks.Open(CommonDialog.filename)
  193.     
  194. For WSheetno = 1 To WBOOK.Worksheets.count
  195.    'loads the no. of sheets in combo1
  196.    Combo1.AddItem "Sheet" & (WSheetno)
  197. Next
  198.  
  199. 'close XLS file w/o saving
  200. WBOOK.Close False
  201. 'quit excel
  202. XLS.Quit
  203.  
  204. End Sub
  205.  
  206. Private Sub load_to_grid()
  207. On Error Resume Next:
  208. If CommonDialog.filename <> "" Then
  209.  
  210.     Set XLS = CreateObject("Excel.Application")
  211.     Set WBOOK = XLS.Workbooks.Open(CommonDialog.filename)
  212.     'Set the WSHEET variable to the selected worksheet
  213.     Set WSHEET = WBOOK.Worksheets(Combo1.List(Combo1.ListIndex))
  214.     'Get the used range of the current worksheet
  215.     Set RNG = WSHEET.UsedRange
  216.     'load the no. of excel columns to counter
  217.     COUNTER = RNG.Columns.count
  218.     
  219.     'Configure the grid to display data
  220.     MSFlexGrid.Clear
  221.     MSFlexGrid.FixedCols = 0
  222.     MSFlexGrid.FixedRows = 0
  223.     MSFlexGrid.Cols = RNG.Columns.count
  224.     MSFlexGrid.Rows = RNG.Rows.count
  225.       
  226.     'loads data of XLS file to the grid
  227.     For R = 0 To MSFlexGrid.Rows - 1
  228.         MSFlexGrid.Row = R
  229.         For C = 0 To MSFlexGrid.Cols - 1
  230.             MSFlexGrid.Col = C
  231.             MSFlexGrid.Text = WSHEET.Cells(R + 1, C + 1).Value
  232.         Next
  233.     Next
  234.     'close XLS file w/o saving
  235.     WBOOK.Close False
  236.     'quit excel
  237.     XLS.Quit
  238.     
  239. End If
  240. End Sub
  241.  
  242. Private Sub Save_to_database_Click() 'save to database
  243.  
  244. On Error Resume Next:
  245. 'adds recordset equal to excel column
  246. For cntfields = 1 To COUNTER
  247.     db.Execute ("alter table TEST_TABLE " _
  248.     & "add column " & "A" & cntfields & " text")
  249. Next cntfields
  250.  
  251.  
  252. Data1.Refresh
  253. db.Recordsets.Refresh
  254. 'open table
  255. Set rs = db.OpenRecordset("TEST_TABLE", dbOpenDynaset)
  256.  
  257.        For R = 0 To MSFlexGrid.Rows - 1
  258.             MSFlexGrid.Row = R
  259.           
  260.             rs.AddNew 'adds data from grid to the database
  261.             For C = 0 To MSFlexGrid.Cols - 1
  262.                 MSFlexGrid.Col = C
  263.                 rs.Fields(C) = MSFlexGrid.Text
  264.             Next
  265.             rs.Update
  266.          
  267.         Next
  268.  
  269.     MsgBox "RECORD SAVE", vbInformation, "Save"
  270.     Save_to_database.Enabled = False
  271.     
  272. End Sub
  273.  
  274. Private Sub Form_Load()
  275. On Error Resume Next:
  276. 'initialize database
  277. Set db = OpenDatabase(App.Path & "\TEST_DB.mdb")
  278. 'delete test_table from database
  279. db.Execute ("DROP TABLE TEST_TABLE")
  280. Data1.Refresh
  281. 'creates new table named as test_table
  282. db.Execute ("CREATE TABLE TEST_TABLE (A1 TEXT)")
  283. Data1.Refresh
  284. End Sub
  285.  
  286.