473,614 Members | 2,351 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Vb6.0 help urgent

19 New Member
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@yah oo.com


Thanks
Jul 6 '06 #1
4 26469
sashi
1,754 Recognized Expert Top Contributor
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.Connectio n - 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
kumar_ps
19 New Member
Thank u very much yaa sashi
Jul 7 '06 #3
bharathi228
28 New Member
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 Recognized Expert Expert
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
2415
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 actually the name of a form element on the parent window. On the new page, I have this javascript: ---- var var2 = location.search.substring(9); document.write(var2)
9
4297
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 data format is european DD.MM.YYYY (for days) and 01.MM.YYYY (for months). The input format is not recognizable as a DATE input to a DB2 LOAD for
8
5231
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 developing a web-based application, one part of which involves allowing the user the ability to page through transaction "history" information. The _summary_ history table will have the following fields: ServiceName, Date, User-Ref1, User-Ref2,...
28
3024
by: Tamir Khason | last post by:
Follwing the struct: public struct TpSomeMsgRep { public uint SomeId;
16
2922
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: http://www.mywebsite.org/WebResource.axd?d=5WvLfhnJp5Lc8WhQSD4gdA2&t=632614619884218750 -------------------------------------------------------------------------------- System.Security.Cryptography.CryptographicException: Padding is invalid and cannot be removed. at...
7
7238
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" id="d_links"> multiple <a href="hello.aspx?q=something">something</a><a href="hello.aspx?q=something1">something1</a><a
33
3396
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
2762
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 partially in c and partially in fortran. I guess i need to change host specific files to make it working. I wonder if standard header files are going to change in this case.my current windows compiler doesn't have sys/resource.h but unix compiler...
3
6455
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 the socket is missing the last character (line feed). When the same text is sent without the urgent flag set, all of the characters are read. I'm reading the data using the blocking read call of the network stream class. The .NET...
7
5965
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 it? If not, how do I have to change my deployment strategy? Thanks!
0
8197
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8640
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8589
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8443
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7114
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6093
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5548
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4136
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1438
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.