Welcome to Dream.In.Code
Become a VB Expert!

Join 149,939 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,426 people online right now. Registration is fast and FREE... Join Now!




Removing records from a database

2 Pages V  1 2 >  
Reply to this topicStart new topic

Removing records from a database, Visual Basic 6.0

Sever
7 Jan, 2008 - 03:38 PM
Post #1

New D.I.C Head
*

Joined: 7 Jan, 2008
Posts: 7

I've looked on the internet for hours, searching for a proper tutorial that made sense to me.

Anyway, I have created a small database as a minor practice-project.

However, I cannot seem to get the removal function right. If someone could point me in the direction of a proper tutorial, or if the can give some small bit of advice, that would be great.

Here is the code.

CODE


Option Explicit

Dim Length As Integer
Dim total As Integer
Dim File As Integer
Private intPosition As Integer

Private Type Names
    Firstname As String * 30
    Lastname As String * 30
    Telephone As String * 30
    Address As String * 30
End Type

Dim udtList(1 To 99999) As Names
Dim tempNames As Names
Public Function GetNames()
Dim s As Integer
Dim i As Integer
Dim recnumber As Integer

---------------------------------------------

Private Sub cmdRemove_Click()
Dim TEMP As String
Dim TEMP2 As String
Dim TEMP3 As String
Dim TEMP4 As String
Dim F As Integer
Dim value As String
Dim LOOKFOR As String
Dim POS As Integer
Dim x As Integer

LOOKFOR = InputBox("Enter the last name, first name, address, or telephone number of the record you want to delete.")

POS = 0
For x = 1 To total
    If Trim(udtList(x).Lastname) = LOOKFOR Or Trim(udtList(x).Firstname) = LOOKFOR Or Trim(udtList(x).Address) = LOOKFOR Or Trim(udtList(x).Telephone) = LOOKFOR Then
        POS = 1
        Exit For
    End If
Next x
If POS = 0 Then
MsgBox (LOOKFOR & " was not found.")
End If
txtSearch.Text = ""

If POS = 1 Then
TEMP = udtList(x).Lastname
TEMP2 = udtList(x).Firstname
TEMP3 = udtList(x).Address
TEMP4 = udtList(x).Telephone
udtList(x).Lastname = udtList(total).Lastname
udtList(x).Firstname = udtList(total).Firstname
udtList(x).Address = udtList(total).Address
udtList(x).Telephone = udtList(total).Telephone
udtList(total).Lastname = TEMP
udtList(total).Firstname = TEMP2
udtList(total).Address = TEMP3
udtList(total).Telephone = TEMP4
End If



This is basically the code I used, without all the other functions that are irrelevant.

Basically, I tried to use something like my sort function, in order to make the selected record turn into the very last record, and it would then somehow cut out the record, however I lack the knowledge to do it.

The forum search function didn't help either.

This code is obviously not finished, because I can't do it ><;;

Thanks

This post has been edited by Sever: 7 Jan, 2008 - 03:39 PM
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Removing Records From A Database
8 Jan, 2008 - 07:02 AM
Post #2

 101010101
Group Icon

Joined: 3 Aug, 2006
Posts: 1,812



Thanked: 1 times
Dream Kudos: 755
My Contributions
Are you using an ADODC to connect to the database?
If you have, all you have to do is use an SQL statement to search for your record. The use the Delete method under ADODC.RecordSet
Code is similar for ADODB as well.
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Removing Records From A Database
8 Jan, 2008 - 07:54 AM
Post #3

 101010101
Group Icon

Joined: 3 Aug, 2006
Posts: 1,812



Thanked: 1 times
Dream Kudos: 755
My Contributions
You could also read these tutorials:
VB Data Controls and usage.
Advanced Data Access through VB code using ADODB
ADODB application Workshop
OleDb Basics in VB.Net
Connecting to a SQL Server database using ADODB
User is offlineProfile CardPM
+Quote Post

Sever
RE: Removing Records From A Database
8 Jan, 2008 - 05:22 PM
Post #4

New D.I.C Head
*

Joined: 7 Jan, 2008
Posts: 7

I changed my deleter function greatly.

Now, under the advice of a friend, I have this:

CODE

Public Function Remover(ByVal Gone As Integer)
Dim i As Integer
Dim nextlen As Integer
Dim temp() As Names
Dim C As Integer
Dim t1 As String
Dim t2 As String
Dim t3 As String
Dim t4 As String

C = frmPhone.Listview1.SelectedItem.Index

udtList(C).Lastname = ""
udtList(C).Firstname = ""
udtList(C).Address = ""
udtList(C).Telephone = ""

For i = 1 To total
t1 = udtList(C).Lastname
t2 = udtList(C).Firstname
t3 = udtList(C).Address
t4 = udtList(C).Telephone
udtList(C).Lastname = udtList(C + 1).Lastname
udtList(C).Firstname = udtList(C + 1).Firstname
udtList(C).Address = udtList(C + 1).Address
udtList(C).Telephone = udtList(C + 1).Telephone
udtList(C + 1).Lastname = t1
udtList(C + 1).Firstname = t2
udtList(C + 1).Address = t3
udtList(C + 1).Telephone = t4
Next i

total = total
Saver
Updater



The records still show. It shows up as blank.

How do I permanently get rid of them from the file that it runs off of?

This post has been edited by Sever: 8 Jan, 2008 - 06:41 PM
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Removing Records From A Database
8 Jan, 2008 - 06:46 PM
Post #5

 101010101
Group Icon

Joined: 3 Aug, 2006
Posts: 1,812



Thanked: 1 times
Dream Kudos: 755
My Contributions
From what I can see, you are just deleting the contents of a listbox. This will not remove the record from the database. How did you connect to the database? You will have to answer that question first.

User is offlineProfile CardPM
+Quote Post

Sever
RE: Removing Records From A Database
8 Jan, 2008 - 07:05 PM
Post #6

New D.I.C Head
*

Joined: 7 Jan, 2008
Posts: 7

Just a random access file.

CODE

File = freefile
Length = LenB(tempNames)

Open "path" for random as #1 len = Length

For x = 1 to total
Get #1, x, udtList(x)
Next x

Close #1


It's my own personal project and as a beginner, I don't want to have to deal with something too complicated.

And yes, I heard that there is no possible way to physically delete Random Access records. So I planned on hiding them.

One thing I forgot, however, is that even when you define something as "", it still adds 30 spaces after, because thats how I defined my private types. T_T

If there was some way around that, I wouldn't be having trouble with this... ><;;

This post has been edited by Sever: 8 Jan, 2008 - 07:30 PM
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Removing Records From A Database
9 Jan, 2008 - 06:51 AM
Post #7

 101010101
Group Icon

Joined: 3 Aug, 2006
Posts: 1,812



Thanked: 1 times
Dream Kudos: 755
My Contributions
I was under the impression that you are using something like MS Access, SQL Server or Oracle smile.gif.
Here's a link that will help you with random access files smile.gif
Open for Random in Visual Basic- Gives a nice detailed explanation on random access files. See the last section for deleting records.
Also here's an MSDN Link:
Click Here
Hope this helps smile.gif.

This post has been edited by Louisda16th: 9 Jan, 2008 - 06:52 AM
User is offlineProfile CardPM
+Quote Post

Sever
RE: Removing Records From A Database
9 Jan, 2008 - 02:45 PM
Post #8

New D.I.C Head
*

Joined: 7 Jan, 2008
Posts: 7

Yup, that was very helpful thanks.

I managed to finish the code, and here is how it now looks:

The entire Delete function:

CODE

Public Function Remover(ByVal Gone As Integer)
File = FreeFile
filetemp = FreeFile
Length = LenB(tempNames)
Open "D:\Database\Namename.txt" For Random As #1 Len = Length

Dim j as integer
For j = 1 To total
    Get #1, j, udtList(j)
Next j
Close #1

Open "D:\Database\Temp.txt" For Random As #2 Len = Length
For j = 1 To (Gone - 1)
    Put #2, j, udtList(j)
Next j

For j = (Gone + 1) To total
    Put #2, (j - 1), udtList(j)
Next j
Close #2

Kill "D:\Database\Namename.txt"
Name "D:\Database\Temp.txt" As "D:\Assignment seven comp sci 1\Namename.txt"
Call cmdView_Click


Thanks a lot, I understand how the delete function works now.
:3

This post has been edited by Sever: 9 Jan, 2008 - 02:47 PM
User is offlineProfile CardPM
+Quote Post

skyhawk133
RE: Removing Records From A Database
9 Jan, 2008 - 02:46 PM
Post #9

Head DIC Head
Group Icon

Joined: 17 Mar, 2001
Posts: 15,272



Thanked: 61 times
Dream Kudos: 1650
Expert In: Web Development

My Contributions
Glad we could be of assistance Sever. You're always welcome to call on us for help with anything!

Welcome to dream.in.code.
User is online!Profile CardPM
+Quote Post

Louisda16th
RE: Removing Records From A Database
9 Jan, 2008 - 11:12 PM
Post #10

 101010101
Group Icon

Joined: 3 Aug, 2006
Posts: 1,812



Thanked: 1 times
Dream Kudos: 755
My Contributions
QUOTE(Sever @ 10 Jan, 2008 - 04:15 AM) *

Thanks a lot, I understand how the delete function works now.
:3

And thanks to you, I learned how to use random access files smile.gif.
User is offlineProfile CardPM
+Quote Post

Sever
RE: Removing Records From A Database
13 Jan, 2008 - 11:29 AM
Post #11

New D.I.C Head
*

Joined: 7 Jan, 2008
Posts: 7

Right, so now I have a new question. Lets say I want to make a word-processor type program. How exactly to I make every word on the list its own record.

For example.

"My name is Sever."

record(1) = My
record(2) = name
record(3) = is
record(4) = Sever

I need it this way, so I can make a search function for certain words.

Also, another question, totally irrelevant to my previous one. I make an application, that runs a program...lets say the game "Halo PC". Now, if I want it to run the program off a USB stick, how would I do it, if the original program is not installed on the computer?

Do I copy all the files in "Program files\Microsoft Games\Halo" folder, and paste them in the USB, and then use app.path to run the exe?

Thirdly, how do I incorporate a telephone function, that uses skype to call people's phones.

I'm kind of making a "universal program", because I have a lot of annoyances due to scattered files and shortcuts.

I think it'd be kind of fun to make one.

This post has been edited by Sever: 13 Jan, 2008 - 11:29 AM
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Removing Records From A Database
14 Jan, 2008 - 10:55 AM
Post #12

 101010101
Group Icon

Joined: 3 Aug, 2006
Posts: 1,812



Thanked: 1 times
Dream Kudos: 755
My Contributions
Here's partly the answer to your first question:
Check this example out.
CODE

Dim finish As Integer
Dim start As Integer
'First word
start = 1
finish = InStr(start, Text1.Text, " ")
MsgBox (Mid(Text1.Text, start, finish - start))
'Next word
start = finish + 1
finish = InStr(start, Text1.Text, " ")
MsgBox (Mid(Text1.Text, start, finish - start))

As you can see, this code extracts each word. Use this kind of structure in a loop.
This code as quite a few bugs however: You have to have two words separated by only one space. Also the last word should be followed by a space. Try fixing this yourself.

Hope this helps smile.gif
PS:
I'm not sure how the other two functions you want could be done. Possibly, running from a USB stick will require you to use API functions.
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 04:34PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month