473,378 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Vb6.0 help urgent

i am new for vb6.0. i am using vb6.0 and msaccess. i have written following code for add new records to database table

Expand|Select|Wrap|Line Numbers
  1.  Private Sub Command1_Click() 
  2. On Error Resume Next
  3. If Trim(Text2.Text) = Trim(Text3.Text) Then
  4. rs.MoveFirst
  5. While Not rs.EOF
  6. If Trim(Text1.Text) = Trim(rs.Fields(0)) Then
  7. MsgBox "You are already exist"
  8. Text1.Text = ""
  9. Text2.Text = ""
  10. End If
  11. rs.MoveNext
  12. Wend
  13. If rs.EOF Then
  14. rs.AddNew
  15. rs!uid = Trim(Text1.Text)
  16. rs!pwd = Trim(Text2.Text)
  17.  
  18. rs.Update
  19. MsgBox "New user created"
  20. End If
  21. Else
  22. MsgBox "Please correct Password"
  23. Text2.Text = ""
  24. Text3.Text = ""
  25. End If
  26. End Sub
  27.  
  28.  
present i need code for delete records fom the table condition is
"If Trim(Text1.Text) = Trim(rs.Fields(0)) Then click delete button automatically delete that record.....


Plz urgent help me

mailid is koti_bujji1@yahoo.com


Thanks
Jul 6 '06 #1
4 26449
sashi
1,754 Expert 1GB
Hi Kumar,

below are some sample codes that allow you to add, edit & delete records from database.. both the DAO & ADO samples are included.. good luck my fren.. :)

This is not about how to create or design a database, it is about how to connect to a database and manipulate a database using VB. It will work (with some minor alterations) in VBA as well.

There are several ways of connecting to a database (for example, Access), via data bound controls, DAO or ADO. On the whole I do not use data bound controls because I like to keep control of what is happening to the data so the rest is about DAO/ADO.

To start with you need to create a few variables of the following types


Workspace ADODB.Connection - This is required if you are using Transaction Processes (I will explain later)
Database - This connects to the database Recordset
ADODB.Recordset - This is the table/query level variable
Field ADODB.Field - This allows us to get info about the fields
Connecting to a Database
With an Access database it is possible to connect to the database in 2 ways, JET or ODBC. Personally I use ODBC because the file management is easier; to change the filename or path just use the ODBC administrator in the control panel.

Note: These examples are here to show what to do very simply, some of the commands have more options than are shown so please review the help files for more details.

DAO example - JET Connection

Expand|Select|Wrap|Line Numbers
  1. Dim ws as Workspace
  2. Dim db as Database
  3.  
  4. Set ws=DBEngine.Workspaces(0)
  5. set db=ws.OpenDatabase({databasepath and name})
  6.  
DAO example - ODBC Connection

Expand|Select|Wrap|Line Numbers
  1. Dim ws as Workspace
  2. dim db as database
  3. dim strConnection as string
  4.  
  5. set ws=DBEngine.Workspaces(0)
  6. let strConnection= "ODBC;DSN=" & DatabaseName & ";UID=" & UserName 
  7. & ";PWD=" & UserPassword
  8. set db=ws.OpenDatabase("", False, False, strConnection)
  9.  
ADO Example

Expand|Select|Wrap|Line Numbers
  1. Dim ad as ADODB.Connection
  2.  
  3. set ad=New ADODB.Connection
  4. Let ad.ConnectionString= "ODBC;DSN=" & DatabaseName & ";UID=" & 
  5. UserName & ";PWD=" & UserPassword
  6. ad.Open
  7.  
Opening a Table/Query for Viewing
Now we have the database connection established it is time to look at the data. The following example show how to open a table/query and move through it.

DAO Example

Expand|Select|Wrap|Line Numbers
  1. Dim rs as recordset
  2.  
  3. set rs=db.openrecordset({tablename or SQL})
  4. do while not rs.eof
  5.   'Put the code here for what to do with the information.
  6.   'The field information can be access by the field name
  7.   intID=rs!IDField
  8.   'Or by the order number it is in the list (starting at 0)
  9.   intString=rs.Field(1)
  10.   rs.movenext
  11. loop
  12.  
ADO example

Expand|Select|Wrap|Line Numbers
  1. dim ar as ADODB.recordset
  2.  
  3. set ar=new adodb.recordset
  4. ar.open {SQL Statement}
  5. do while not ar.EOF
  6.   'Put the code here for what to do with the information.
  7.   'The field information can be access by the field name
  8.   intID=ar!IDField
  9.   'Or by the order number it is in the list (starting at 0)
  10.   intString=ar.Field(1).value
  11.   ar.movenext
  12. loop
  13.  
Change a Record
To edit/add/delete a record we can do it either using SQL or directly. Both DAO and ADO use the execute method for doing updates by SQL.

DAO

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim rs as recordset
  3.  
  4. set rs=db.openrecordset({tablename or SQL})
  5. rs.execute "INSERT INTO tb(ID,Name) VALUES (10,Anne)"
  6.  
ADO

Expand|Select|Wrap|Line Numbers
  1. dim ar as ADODB.recordset
  2.  
  3. set ar=new adodb.recordset
  4. ar.open {SQL Statement}
  5. ar.execute "INSERT INTO tb(ID,Name) VALUES (10,Anne)"
  6.  
These examples add a new record to the database directly.

DAO - Add New Record

Expand|Select|Wrap|Line Numbers
  1. Dim rs as recordset
  2.  
  3. set rs=db.openrecordset({tablename or SQL})
  4. rs.addnew
  5. rs!ID=intID
  6. rs!Name=strName
  7. rs.update
  8.  
ADO - Add new record

Expand|Select|Wrap|Line Numbers
  1. dim ar as ADODB.recordset
  2.  
  3. set ar=new adodb.recordset
  4. ar.open {SQL Statement}
  5. ar.addnew
  6. ar!ID=intID
  7. ar!Name=strName
  8. ar.update
  9.  
These examples show how to edit a record directly, after the recordset is open it checks that there is a record meeting the criteria in the open SQL. If not it creates one.

DAO - Edit record

Expand|Select|Wrap|Line Numbers
  1. Dim rs as recordset
  2.  
  3. set rs=db.openrecordset("SELECT * FROM Tb WHERE tdID=10")
  4. if rs.eof then
  5.   rs.addnew
  6. else
  7.   rs.edit
  8. end if
  9. rs!ID=intID
  10. rs!Name=strName
  11. rs.update
  12.  
ADO - Edit Record

Expand|Select|Wrap|Line Numbers
  1. dim ar as ADODB.recordset
  2.  
  3. set ar=new adodb.recordset
  4. ar.open "SELECT * FROM Tb WHERE tdID=10"
  5. if ar.eof then
  6.   ar.addnew
  7. else
  8.   ar.edit
  9. end if
  10. ar!ID=intID
  11. ar!Name=strName
  12. ar.update
  13.  
These examples show how to delete a record directly, after the recordset is open it checks that there is a record meeting the criteria in the open SQL. If not it does not do a delete.

DAO - Delete Record

Expand|Select|Wrap|Line Numbers
  1. Dim rs as recordset
  2.  
  3. set rs=db.openrecordset("SELECT * FROM Tb WHERE tdID=10")
  4. if not rs.eof then
  5.   rs.delete
  6. end if
  7.  
ADO - Delete Record

Expand|Select|Wrap|Line Numbers
  1. Dim ar as ADODB.recordset
  2.  
  3. set ar=new adodb.recordset
  4. ar.open "SELECT * FROM Tb WHERE tdID=10"
  5. if not ar.eof then
  6.   ar.delete
  7. end if
  8.  
Note: If you open an object when you have finished with it, close it and set it to nothing. For example...

Expand|Select|Wrap|Line Numbers
  1.     rs.close
  2.     set rs=nothing
  3.  
This is good programming practice and clears the memory.
Jul 6 '06 #2
Thank u very much yaa sashi
Jul 7 '06 #3
Hello Sashi Sir,


Please explain the above code (ADO Examples)using dataset instead of using recordset.

iam new to vb 6.0

workspace,
.index
.seek
.addnew
.edit
.update
.movefirst
.movelast
.moveprevious


how can i use the above using dataset.pls help me with examples.
Jul 22 '08 #4
debasisdas
8,127 Expert 4TB
you need to use the corresponding methods of .NET.

check in Howto section for related discussions.
Jul 23 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Rob | last post by:
I have a form - when you click the submit button, it appends a variable to the URL (e.g. xyz.cgi?inputID=some_dynamic_variable) It also opens a new page. Now, that some_dynamic_variable is...
9
by: Stefan Bauer | last post by:
Hi NG, we've got a very urgent problem... :( We are importing data with the LOAD utility. The input DATE field data is in the format DDMMYYYY (for days) and MMYYYY (for months). The target...
8
by: Mike | last post by:
Hello, I have a few rather urgent questions that I hope someone can help with (I need to figure this out prior to a meeting tomorrow.) First, a bit of background: The company I work for is...
28
by: Tamir Khason | last post by:
Follwing the struct: public struct TpSomeMsgRep { public uint SomeId;
16
by: | last post by:
Hi all, I have a website running on beta 2.0 on server 2003 web sp1 and I keep getting the following error:- Error In:...
7
by: zeyais | last post by:
Here is my HTML: <style> ..leftcolumn{float:left;width:300px;border: 1px solid #ccc} ..rtcolumn{float:left;width:600px;border: 1px solid #ccc} </style> <body> <div class="leftcolumn"...
33
by: dembla | last post by:
Hey Frnds can anyone help me in this i need a program in 'c' PROGRAM to print NxN Matrix 9 1 8 1 2 3 2 7 3 as 4 5 6 6 4 5 7 8 9 in sorted form
8
by: ginnisharma1 | last post by:
Hi All, I am very new to C language and I got really big assignment in my work.I am wondering if anyone can help me.........I need to port compiler from unix to windows and compiler is written...
3
by: N. Spiker | last post by:
I am attempting to receive a single TCP packet with some text ending with carriage return and line feed characters. When the text is send and the packet has the urgent flag set, the text read from...
7
by: Cirene | last post by:
I used to use the Web Deployment Project with my VS2005 projects. Now I've fully upgraded to VS2008. Do I have to download a new version of the Web Deployment Project? If so where can I find...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.