home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD3898392000.psc / Form1.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  2000-03-09  |  17.1 KB  |  567 lines

  1. VERSION 5.00
  2. Begin VB.Form Form1 
  3.    Caption         =   "Form1"
  4.    ClientHeight    =   5835
  5.    ClientLeft      =   60
  6.    ClientTop       =   345
  7.    ClientWidth     =   11880
  8.    LinkTopic       =   "Form1"
  9.    ScaleHeight     =   5835
  10.    ScaleWidth      =   11880
  11.    StartUpPosition =   3  'Windows Default
  12.    Begin VB.Frame Frame4 
  13.       Caption         =   "Add / Delete Field from table"
  14.       Height          =   4095
  15.       Left            =   6840
  16.       TabIndex        =   23
  17.       Top             =   0
  18.       Width           =   4935
  19.       Begin VB.CommandButton Command9 
  20.          Caption         =   "List Tables"
  21.          Height          =   375
  22.          Left            =   3120
  23.          TabIndex        =   33
  24.          Top             =   1440
  25.          Width           =   1695
  26.       End
  27.       Begin VB.ListBox List2 
  28.          Height          =   2400
  29.          Left            =   120
  30.          TabIndex        =   32
  31.          Top             =   1440
  32.          Width           =   2775
  33.       End
  34.       Begin VB.CommandButton Command7 
  35.          Caption         =   "Delete Field"
  36.          Height          =   255
  37.          Left            =   1680
  38.          TabIndex        =   29
  39.          Top             =   840
  40.          Width           =   1335
  41.       End
  42.       Begin VB.TextBox Text10 
  43.          Height          =   285
  44.          Left            =   120
  45.          TabIndex        =   28
  46.          Top             =   480
  47.          Width           =   2175
  48.       End
  49.       Begin VB.CommandButton Command6 
  50.          Caption         =   "Add Field"
  51.          Height          =   255
  52.          Left            =   120
  53.          TabIndex        =   26
  54.          Top             =   840
  55.          Width           =   1335
  56.       End
  57.       Begin VB.TextBox Text9 
  58.          Height          =   285
  59.          Left            =   2880
  60.          TabIndex        =   24
  61.          Top             =   480
  62.          Width           =   1935
  63.       End
  64.       Begin VB.Label Label8 
  65.          Caption         =   "Enter Table Name"
  66.          Height          =   255
  67.          Left            =   120
  68.          TabIndex        =   27
  69.          Top             =   240
  70.          Width           =   1935
  71.       End
  72.       Begin VB.Label Label7 
  73.          Caption         =   "Field Name"
  74.          Height          =   255
  75.          Left            =   2880
  76.          TabIndex        =   25
  77.          Top             =   240
  78.          Width           =   1335
  79.       End
  80.    End
  81.    Begin VB.Frame Frame3 
  82.       Caption         =   "Add / Delete Table"
  83.       Height          =   1575
  84.       Left            =   0
  85.       TabIndex        =   15
  86.       Top             =   4200
  87.       Width           =   6735
  88.       Begin VB.CommandButton Command8 
  89.          Caption         =   "Delete Table"
  90.          Height          =   375
  91.          Left            =   120
  92.          TabIndex        =   30
  93.          Top             =   960
  94.          Width           =   1455
  95.       End
  96.       Begin VB.TextBox Text8 
  97.          Height          =   285
  98.          Left            =   3120
  99.          TabIndex        =   22
  100.          Top             =   840
  101.          Width           =   2295
  102.       End
  103.       Begin VB.TextBox Text7 
  104.          Height          =   285
  105.          Left            =   3120
  106.          TabIndex        =   21
  107.          Top             =   600
  108.          Width           =   2295
  109.       End
  110.       Begin VB.TextBox Text6 
  111.          Height          =   285
  112.          Left            =   3120
  113.          TabIndex        =   19
  114.          Top             =   360
  115.          Width           =   2295
  116.       End
  117.       Begin VB.CommandButton Command5 
  118.          Caption         =   "Add Table"
  119.          Height          =   375
  120.          Left            =   5520
  121.          TabIndex        =   18
  122.          Top             =   360
  123.          Width           =   1095
  124.       End
  125.       Begin VB.TextBox Text5 
  126.          Height          =   285
  127.          Left            =   120
  128.          TabIndex        =   16
  129.          Top             =   480
  130.          Width           =   2055
  131.       End
  132.       Begin VB.Label Label9 
  133.          Caption         =   "Only Enter Fields if your adding a table"
  134.          Height          =   255
  135.          Left            =   3120
  136.          TabIndex        =   31
  137.          Top             =   1200
  138.          Width           =   3255
  139.       End
  140.       Begin VB.Label Label6 
  141.          Caption         =   "Enter Fields to Add"
  142.          Height          =   255
  143.          Left            =   3120
  144.          TabIndex        =   20
  145.          Top             =   120
  146.          Width           =   1695
  147.       End
  148.       Begin VB.Label Label5 
  149.          Caption         =   "Enter Table Name"
  150.          Height          =   255
  151.          Left            =   120
  152.          TabIndex        =   17
  153.          Top             =   240
  154.          Width           =   1455
  155.       End
  156.    End
  157.    Begin VB.Frame Frame2 
  158.       Caption         =   "List / Edit / Delete Record"
  159.       Height          =   2895
  160.       Left            =   0
  161.       TabIndex        =   6
  162.       Top             =   1200
  163.       Width           =   6735
  164.       Begin VB.TextBox Text4 
  165.          Height          =   285
  166.          Left            =   3720
  167.          TabIndex        =   12
  168.          Top             =   1320
  169.          Width           =   2655
  170.       End
  171.       Begin VB.TextBox Text3 
  172.          Height          =   285
  173.          Left            =   3720
  174.          TabIndex        =   11
  175.          Top             =   960
  176.          Width           =   2655
  177.       End
  178.       Begin VB.CommandButton Command4 
  179.          Caption         =   "Delete"
  180.          Height          =   375
  181.          Left            =   3240
  182.          TabIndex        =   10
  183.          Top             =   2400
  184.          Width           =   1095
  185.       End
  186.       Begin VB.CommandButton Command3 
  187.          Caption         =   "Change"
  188.          Height          =   375
  189.          Left            =   3240
  190.          TabIndex        =   9
  191.          Top             =   1680
  192.          Width           =   1095
  193.       End
  194.       Begin VB.CommandButton Command2 
  195.          Caption         =   "List"
  196.          Height          =   375
  197.          Left            =   3240
  198.          TabIndex        =   8
  199.          Top             =   240
  200.          Width           =   1095
  201.       End
  202.       Begin VB.ListBox List1 
  203.          Height          =   2595
  204.          Left            =   120
  205.          TabIndex        =   7
  206.          Top             =   240
  207.          Width           =   3015
  208.       End
  209.       Begin VB.Label Label4 
  210.          Caption         =   "E-Mail"
  211.          Height          =   255
  212.          Left            =   3240
  213.          TabIndex        =   14
  214.          Top             =   1320
  215.          Width           =   735
  216.       End
  217.       Begin VB.Label Label3 
  218.          Caption         =   "Name"
  219.          Height          =   255
  220.          Left            =   3240
  221.          TabIndex        =   13
  222.          Top             =   960
  223.          Width           =   975
  224.       End
  225.    End
  226.    Begin VB.Frame Frame1 
  227.       Caption         =   "Add Record"
  228.       Height          =   1095
  229.       Left            =   0
  230.       TabIndex        =   0
  231.       Top             =   0
  232.       Width           =   6735
  233.       Begin VB.CommandButton Command1 
  234.          Caption         =   "Add"
  235.          Height          =   375
  236.          Left            =   4200
  237.          TabIndex        =   5
  238.          Top             =   240
  239.          Width           =   1095
  240.       End
  241.       Begin VB.TextBox Text2 
  242.          Height          =   285
  243.          Left            =   720
  244.          TabIndex        =   4
  245.          Top             =   600
  246.          Width           =   3375
  247.       End
  248.       Begin VB.TextBox Text1 
  249.          Height          =   285
  250.          Left            =   720
  251.          TabIndex        =   1
  252.          Top             =   240
  253.          Width           =   3375
  254.       End
  255.       Begin VB.Label Label2 
  256.          Caption         =   "E-Mail"
  257.          Height          =   255
  258.          Left            =   120
  259.          TabIndex        =   3
  260.          Top             =   600
  261.          Width           =   735
  262.       End
  263.       Begin VB.Label Label1 
  264.          Caption         =   "Name"
  265.          Height          =   255
  266.          Left            =   120
  267.          TabIndex        =   2
  268.          Top             =   240
  269.          Width           =   975
  270.       End
  271.    End
  272. Attribute VB_Name = "Form1"
  273. Attribute VB_GlobalNameSpace = False
  274. Attribute VB_Creatable = False
  275. Attribute VB_PredeclaredId = True
  276. Attribute VB_Exposed = False
  277. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  278. 'Database Tutorial v1.0
  279. 'Dustin Davis
  280. 'VB Live
  281. 'http://www.vblive.com
  282. 'This tutorial is to show you how to talk to databases with pure code
  283. 'no data object. This comes in handy for ASP applications or DHTML projects
  284. 'You will need a reference to DAO 3.6 (only if you use access 2000)
  285. 'and if you dont have it, get the service pack 3 from microsoft.com
  286. 'If you use access 98 or lower, you can use DAO 3.5
  287. 'Questions or comments, please send em to cheater@smallwww.com
  288. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  289. Public Function Add()
  290. 'This function will show you how to add a record to the database which
  291. 'i have created already with access 2000
  292. 'Dim our variables
  293. Dim DB As Database
  294. Dim RS As Recordset
  295. Dim WS As Workspace
  296. 'This sets a workspace for the database
  297. Set WS = DBEngine.Workspaces(0)
  298. 'this opens the database
  299. Set DB = WS.OpenDatabase(App.Path & "\dbtut.mdb")
  300. 'this opens a table inside the database
  301. Set RS = DB.OpenRecordset("email", dbOpenTable)
  302. 'Tells the database we want to add a new record to the recordset
  303. RS.AddNew
  304. 'Put the data in the proper fields
  305. 'RS is your recordset and ("field_name") points to the field you want to
  306. 'set the data for
  307. RS("Name") = Text1.Text
  308. RS("E-Mail") = Text2.Text
  309. 'Update the database. If you dont, the database will add it, but
  310. 'it wont be visible
  311. RS.Update
  312. 'close the database
  313. DB.Close
  314. End Function
  315. Private Sub Command1_Click()
  316. 'Add to the DB
  317. 'clear the text boxes
  318. Text1.Text = ""
  319. Text2.Text = ""
  320. 'relist everything
  321. End Sub
  322. Private Sub Command2_Click()
  323. 'call the list function
  324. End Sub
  325. Private Sub Command3_Click()
  326. 'call the edit function
  327. 'clear the text boxes
  328. Text3.Text = ""
  329. Text4.Text = ""
  330. 'relist everything
  331. End Sub
  332. Private Sub Command4_Click()
  333. 'call the delete function
  334. Delete
  335. 'relist everything
  336. End Sub
  337. Private Sub Edit()
  338. 'This function will show how to read from the fields
  339. 'Dim our variables
  340. Dim DB As Database
  341. Dim RS As Recordset
  342. Dim WS As Workspace
  343. Dim i As Long
  344. If List1.SelCount = 1 Then
  345.     'we will use this to move to that record
  346.     i = List1.ListIndex
  347.     'This sets a workspace for the database
  348.     Set WS = DBEngine.Workspaces(0)
  349.     'this opens the database
  350.     Set DB = WS.OpenDatabase(App.Path & "\dbtut.mdb")
  351.     'this opens a table inside the database
  352.     Set RS = DB.OpenRecordset("email", dbOpenTable)
  353.     'Move to the record. NOTE: if it is off and not changing
  354.     'the correct record, try adding one to i
  355.     RS.Move i
  356.     'Tell the Database you want to change the info
  357.     'Or in other words, opens edit mode
  358.     RS.Edit
  359.     'change the info
  360.     RS("Name") = Text3.Text
  361.     RS("E-Mail") = Text4.Text
  362.     'Update the DB
  363.     RS.Update
  364.     'close the database
  365.     DB.Close
  366.     MsgBox "Select a name from the list box"
  367.     Exit Sub
  368. End If
  369. End Sub
  370. Private Function List()
  371. 'This function will show you how to list all of the records
  372. 'in a table
  373. 'Dim our variables
  374. Dim DB As Database
  375. Dim RS As Recordset
  376. Dim WS As Workspace
  377. Dim Max As Long
  378. 'This sets a workspace for the database
  379. Set WS = DBEngine.Workspaces(0)
  380. 'this opens the database
  381. Set DB = WS.OpenDatabase(App.Path & "\dbtut.mdb")
  382. 'this opens a table inside the database
  383. Set RS = DB.OpenRecordset("email", dbOpenTable)
  384. 'Get how manby records are in the table
  385. Max = RS.RecordCount
  386. 'Move to the begining of the file, or you can do
  387. 'RS.MoveFirst or RS.Move 1, but i prefer this
  388. RS.Move BOF
  389. 'clear the list
  390. List1.Clear
  391. 'do the loop
  392. For i = 1 To Max
  393.     'Add the data from the fields to the listbox. Notice i used
  394.     'two different methods. One is kind of a shortcut RS!Name
  395.     'is easy and simple. But, if you want to put in a name
  396.     'that has a Dash '-' or use a varable like;
  397.     'dim FieldName as String
  398.     'FieldName = "E-Mail"
  399.     'rs(FieldName)
  400.     'You can not do this with the RS!FieldName Method. It dont
  401.     'work.
  402.     List1.AddItem RS!Name & "," & RS("E-Mail")
  403.     'move to next record. You can do different things such as
  404.     'RS.MoveNext or RS.Move i, i will use
  405.     RS.MoveNext
  406. Next i
  407. End Function
  408. Public Function Delete()
  409. 'This function will show how to delete records
  410. 'Dim our variables
  411. Dim DB As Database
  412. Dim RS As Recordset
  413. Dim WS As Workspace
  414. Dim i As Long
  415. If List1.SelCount = 1 Then
  416.     i = List1.ListIndex
  417.     'This sets a workspace for the database
  418.     Set WS = DBEngine.Workspaces(0)
  419.     'this opens the database
  420.     Set DB = WS.OpenDatabase(App.Path & "\dbtut.mdb")
  421.     'this opens a table inside the database
  422.     Set RS = DB.OpenRecordset("email", dbOpenTable)
  423.     'Move to the record
  424.     RS.Move i
  425.     'Tell the Database you want to delete the record
  426.     RS.Delete
  427.     'Simple isnt it?!
  428.            
  429.     'close the database
  430.     DB.Close
  431.     MsgBox "Select a name from the list box"
  432.     Exit Function
  433. End If
  434. End Function
  435. Private Sub Command5_Click()
  436. 'Call add table function
  437. Add_Table
  438. End Sub
  439. Public Function Add_Table()
  440. 'This function will show how to delete records
  441. 'Dim our variables
  442. Dim DB As Database
  443. Dim WS As Workspace
  444. Dim TD As TableDef
  445. Dim FD1 As Field
  446. Dim FD2 As Field
  447. Dim FD3 As Field
  448. 'This sets a workspace for the database
  449. Set WS = DBEngine.Workspaces(0)
  450. 'this opens the database
  451. Set DB = WS.OpenDatabase(App.Path & "\dbtut.mdb")
  452. 'Set the table info
  453. Set TD = DB.CreateTableDef(Text5.Text)
  454.            
  455. 'create new fields and bind it to the table.
  456. 'For the dbText, you can use dbInteger or whatever else
  457. 'you wish to set the field type to. I would stick with those
  458. '2 though.
  459. Set FD1 = TD.CreateField(Text6.Text, dbText)
  460. Set FD2 = TD.CreateField(Text7.Text, dbText)
  461. Set FD3 = TD.CreateField(Text8.Text, dbText)
  462. 'bind the Fields to the table
  463. TD.Fields.Append FD1
  464. TD.Fields.Append FD2
  465. TD.Fields.Append FD3
  466. 'Now bind the table to the database
  467. DB.TableDefs.Append TD
  468. 'close the database
  469. DB.Close
  470. End Function
  471. Private Sub Command6_Click()
  472. 'Call add field function
  473. Add_Field
  474. End Sub
  475. Public Function Add_Field()
  476. 'This function will show how to delete records
  477. 'Dim our variables
  478. Dim DB As Database
  479. Dim WS As Workspace
  480. Dim TD As TableDef
  481. Dim FD As Field
  482. 'This sets a workspace for the database
  483. Set WS = DBEngine.Workspaces(0)
  484. 'this opens the database
  485. Set DB = WS.OpenDatabase(App.Path & "\dbtut.mdb")
  486. 'Set the table to open
  487. Set TD = DB.TableDefs(Text10.Text)
  488. 'Once again, you can use dbText, or dbInterger
  489. 'or whatever else you wish to set the field type
  490. Set FD = TD.CreateField(Text9.Text, dbText)
  491. 'Bind field to the table
  492. TD.Fields.Append FD
  493. 'close the database
  494. DB.Close
  495. End Function
  496. Public Function Delete_Field()
  497. 'This function will show how to delete records
  498. 'Dim our variables
  499. Dim DB As Database
  500. Dim WS As Workspace
  501. Dim TD As TableDef
  502. Dim FD As Field
  503. 'This sets a workspace for the database
  504. Set WS = DBEngine.Workspaces(0)
  505. 'this opens the database
  506. Set DB = WS.OpenDatabase(App.Path & "\dbtut.mdb")
  507. 'Set the table to open
  508. Set TD = DB.TableDefs(Text10.Text)
  509. 'Erase the field
  510. TD.Fields.Delete Text9.Text
  511. 'close the database
  512. DB.Close
  513. End Function
  514. Private Sub Command7_Click()
  515. 'call the delete field function
  516. Delete_Field
  517. End Sub
  518. Private Sub Command8_Click()
  519. 'call the delete table function
  520. Delete_Table
  521. End Sub
  522. Public Function Delete_Table()
  523. 'This function will show how to delete records
  524. 'Dim our variables
  525. Dim DB As Database
  526. Dim WS As Workspace
  527. Dim TD As TableDef
  528. Dim FD As Field
  529. 'This sets a workspace for the database
  530. Set WS = DBEngine.Workspaces(0)
  531. 'this opens the database
  532. Set DB = WS.OpenDatabase(App.Path & "\dbtut.mdb")
  533. 'Set the table to open
  534. DB.TableDefs.Delete Text5.Text
  535. 'close the database
  536. DB.Close
  537. End Function
  538. Private Sub Command9_Click()
  539. 'call the list tables function
  540. list_Tables
  541. End Sub
  542. Public Function list_Tables()
  543. 'This function will show how to list tables in a database
  544. 'Dim our variables
  545. Dim DB As Database
  546. Dim WS As Workspace
  547. Dim TD As TableDef
  548. Dim Temp As String
  549. Dim Max As Long
  550. 'This sets a workspace for the database
  551. Set WS = DBEngine.Workspaces(0)
  552. 'this opens the database
  553. Set DB = WS.OpenDatabase(App.Path & "\dbtut.mdb")
  554. 'find the number of tables in the database
  555. Max = DB.TableDefs.Count
  556. For i = 0 To Max - 6
  557. 'Take away six from max because the last 5
  558. 'are not for you to mess with
  559. 'select the table
  560. Set TD = DB.TableDefs(i)
  561. 'List the tables in the listbox
  562. List2.AddItem TD.Name
  563. Next i
  564. 'close the database
  565. DB.Close
  566. End Function
  567.