?
Solved

Help to get started with Visual Basic and databases

Posted on 2003-03-20
7
Medium Priority
?
289 Views
Last Modified: 2008-02-01
Hello!

I have made an .mdb access database. A table in it named movies with the columns "MovieID" "Movietitle" "Year" "Genre"
Now i want to add a movie through a vb form. How do I do that? I need help to get started.

Bw Markus
0
Comment
Question by:ZtarDuzt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 2

Expert Comment

by:JoaTex
ID: 8175885
Hi
Try this litle example and studie it and you'll do your job.


Create a DataBase named: Teste.mdb (put your data at c:\Teste.mdb)
3 Fields Indexed By Name
Name,Age,Country (All Text)
name the table as Table1.
enter some data on fields: Ex:
John,25,England
Charles,20,France
Rocky,27,USA

Open a Form1 in your project.
put 2 CommandButtons on it
command1 Named: New, Caption = UpDAte
command2 Named: Delete, Caption = Delete
1 data Control named: Data1
on properties set Databasename = c:\Teste.mdb
            Recordset = Table1
            Visible = False
1 grid control named: DataGrid1 (Microsoft Data Bound Grid Control 5.0)(SP3) In This Example
on project properties DBgrid1 set Data Mode = 0-Bound
                                  Data Source= DAta1
Right mouse click on dbgrid1 control on form1 and select Retrieve Fields.
Right mouse click on dbgrid1 control on form1 and select Properties.
 
Select General:
Caption: Teste - Table1
AllowAddNew,AllowDelete,AllowUpdate,ColumnHeaders,Enabled,MarqueeUnique - All Checkbox = on  
Accept Apply

Select: Columns
Column0(Name),Caption = Name,DataField = Name
Column1(Age),Caption = Name,DataField = Age
Column2(Country),Caption = Name,DataField = Country

Select Layout:
Column = Column0(NAme), Split = Split0, Alignement = 0-Left
Column = Column1(Age), Split = Split0, Alignement = 2 - Center, Apply
Column = Column2(Country), Split = Split0, Alignement = 0-Left

Select Splits:
Correct ScrollBars = 2 - Vertical, Apply
OK

Save your project and run the program. See Size grid. Exit Program. Adjust Grid.
Now, go to data1 Control Properties and at DatabaseName delete the directory.
Transfer the c:\Teste.mdb to your aplication directory.
In this way when you make the exe file or disrtribution of your program de database always shoul be at the same
directory of your exe file.

Put this code on Form1:

Code:

Option Explicit

Private Sub ComDel_Click()
   Data1.Recordset.Delete
   DBGrid1.Refresh
End Sub

Private Sub ComNew_Click()
   Data1.UpdateRecord
End Sub

Private Sub DBGrid1_AfterInsert()
   If DBGrid1.Row = 0 Then Data1.Recordset.AbsolutePosition = 0
End Sub

Private Sub Form_Load()
   Me.Top = (Screen.Height - Me.Height) / 2
   Me.Left = (Screen.Width - Me.Width) / 2
   Data1.DatabaseName = App.Path & "\Teste.mdb"
End Sub

Now you can see that updates and controls are direct from grid. you must try to make some improvements
such as some errors that are going to happen, as for instance duplicate files by same name index, make an error routine,
and try to make DELETE ALL or Search method.

Hope this suites you.
Sorry My English
Jo
0
 

Author Comment

by:ZtarDuzt
ID: 8176503
Thanks but I can´t find the "(Microsoft Data Bound Grid Control 5.0)(SP3)" The thing i have is "Microsoft Data Bound Grid Control 6.0(SP3) (OLEDB)"

When I try to use it I get errormessages.

Can u please add me on ICQ or msn?

ICQ: 76572448
MSN: ztareoo@msn.com
0
 
LVL 2

Accepted Solution

by:
JoaTex earned 180 total points
ID: 8177889
Hi

Create a DataBase named: Teste.mdb (put your data at c:\Teste.mdb)
3 Fields Indexed By Name
Name,Age,Country (All Text)
name the table as Table1.
enter some data on fields: Ex:
John,25,England
Charles,20,France
Rocky,27,USA

Open a Form1 in your project.
put 2 CommandButtons on it
command1 Named: New, Caption = UpDAte
command2 Named: Delete, Caption = Delete
1 Adodc Control named: Adodc1 (Microsoft ADO Data Control 6.0(OLEDB) - MSADODC.OCX)
Right Mouse click on Adodc1 in your form.
Select Adodc Properties
On property Pages -General - Source of Conection
select Use conection string - Click - Build
On Data Link Properties select: Microsoft Jet 3.51 Ole DB Provider
Select Conection - 1. Select or enter a database name - c:\Teste.Mdb
                   2. User name - put Admin
                   click Test Connection - see if it is ok.
Select Advanced - Access permissions - Select first Read, and last write
Click Ok
back to property Pages Select Record Source
Command Type - Select 2-adCmdTable
Table or Stored Procedure Name - select Table1
Click OK
On project1 Properties Adodc1 see if:
recordsource = Table1
ConectionString = Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=c:\Teste.mdb;Mode=Read|Write
CommandType = 2 - CmdTable

Now Put 1 Datagrid - Microsoft DataGrid Control 6.0(OLEDB) MSDATAGRD:OCX
on Visual basic project1 properties datagrid1 select:
     Datasource = Adodc1.
Now Right click in form1 on your datagrid1 and select Retrive Fields and click Yes.
Now Right click in form1 on your datagrid1 and select Properties
On Property Pages - General Select all Left CheckBox and leave the rest.
Select Columns. see if you have:
     Column0(Name)
     Column1(Age)
     Column(Country)
Select Splits - Disable Locked
          On ScrollBars - Select 2- dbgVertical
Click Apply - Click OK.

Now put this litle code in your form1

Code:

Option Explicit

Private Sub ComDel_Click()
   Adodc1.Recordset.Delete
   DataGrid1.Refresh
End Sub

Private Sub ComNew_Click()
   Adodc1.Recordset.Update
   Adodc1.Recordset.MoveLast
   DataGrid1.Refresh
End Sub

Private Sub Form_Load()
   Me.Top = (Screen.Height - Me.Height) / 2
   Me.Left = (Screen.Width - Me.Width) / 2
End Sub

Now run your code and you just start to learn how to Add and delete fields on your database.
studie more on your Msdn how to join datagrid properties and SQL coments to filter columns etc.
Try to play with it a litle. one of this days I'll send more complex example of managing tables, filters and database search by your mail.
Hope it suites you.
Jo
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 120 total points
ID: 8179161
You can use the Data grid control and make it invisible. Then, you can use a list-view to view your table. In case you only need to write data to the table, let's say that you have 4 text-fields on your VB form, where you've entered the ID, name, year and genre of the movie. Let's name these text-fields as mID, mName, mYear and mGenre respectively. Then, say you press on a Submit button and you want that the data should be written to the table:

Private Sub Submit_Click ()

' Data1 is the name of the data control

Data1.DatabaseName = "...." ' the complete path/ name of your .mdb file
Data1.RecordSource = "SELECT * FROM MOVIES WHERE MovieID = " & mID & " ; " '
Data1.Refresh

If Not Data1.Recordset.EOF Then
  MsgBox "The entry for that movie ID already exists. "
Else
  Data1.Database.OpenRecordset ( "MOVIES" ) ' table-name
  With Data1.Recordset
    .AddNew
    ![MovieID] = Val (mID) ' converting to integer
    ![MovieTitle] = mName
    ![Year] = Val (mYear) ' converting to integer
    ![Genre] = mGenre
    .Update
  End With
End If

End Sub


That'll do it!

Mayank.

0
 

Expert Comment

by:CleanupPing
ID: 9446942
ZtarDuzt:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9471999
Recommendation: Split points between mayankeagle and JoaTex.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Six Sigma Control Plans
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question