Removing records from a database

Visual Basic 6.0

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 3330 Views - Last Post: 18 January 2008 - 05:04 PM Rate Topic: -----

#1 Sever  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 07-January 08

Removing records from a database

Posted 07 January 2008 - 04:38 PM

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.


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: 07 January 2008 - 04:39 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Removing records from a database

#2 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Removing records from a database

Posted 08 January 2008 - 08:02 AM

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.
Was This Post Helpful? 0
  • +
  • -

#3 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Removing records from a database

Posted 08 January 2008 - 08:54 AM

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
Was This Post Helpful? 0
  • +
  • -

#4 Sever  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 07-January 08

Re: Removing records from a database

Posted 08 January 2008 - 06:22 PM

I changed my deleter function greatly.

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

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: 08 January 2008 - 07:41 PM

Was This Post Helpful? 0
  • +
  • -

#5 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Removing records from a database

Posted 08 January 2008 - 07:46 PM

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.
Was This Post Helpful? 0
  • +
  • -

#6 Sever  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 07-January 08

Re: Removing records from a database

Posted 08 January 2008 - 08:05 PM

Just a random access file.

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: 08 January 2008 - 08:30 PM

Was This Post Helpful? 0
  • +
  • -

#7 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Removing records from a database

Posted 09 January 2008 - 07:51 AM

I was under the impression that you are using something like MS Access, SQL Server or Oracle :).
Here's a link that will help you with random access files :)
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 :).

This post has been edited by Louisda16th: 09 January 2008 - 07:52 AM

Was This Post Helpful? 0
  • +
  • -

#8 Sever  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 07-January 08

Re: Removing records from a database

Posted 09 January 2008 - 03:45 PM

Yup, that was very helpful thanks.

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

The entire Delete function:

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: 09 January 2008 - 03:47 PM

Was This Post Helpful? 0
  • +
  • -

#9 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1875
  • View blog
  • Posts: 20,284
  • Joined: 17-March 01

Re: Removing records from a database

Posted 09 January 2008 - 03:46 PM

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

Welcome to dream.in.code.
Was This Post Helpful? 0
  • +
  • -

#10 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Removing records from a database

Posted 10 January 2008 - 12:12 AM

View PostSever, on 10 Jan, 2008 - 04:15 AM, said:

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

And thanks to you, I learned how to use random access files :).
Was This Post Helpful? 0
  • +
  • -

#11 Sever  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 07-January 08

Re: Removing records from a database

Posted 13 January 2008 - 12:29 PM

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 January 2008 - 12:29 PM

Was This Post Helpful? 0
  • +
  • -

#12 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Removing records from a database

Posted 14 January 2008 - 11:55 AM

Here's partly the answer to your first question:
Check this example out.
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 :)
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.
Was This Post Helpful? 0
  • +
  • -

#13 Sever  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 07-January 08

Re: Removing records from a database

Posted 14 January 2008 - 06:22 PM

It would be awesome if I knew what an API function was. I am a horrid newbie to VB6 XD.

I tried using the shell function, however, it kept saying that I was missing "config.txt", which is odd.

I have a friend to help me with the skype based application.

It's just the file thing...
Was This Post Helpful? 0
  • +
  • -

#14 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Removing records from a database

Posted 14 January 2008 - 09:10 PM

You can Google(Click here) for API

check these links in particular. They give a nice little introduction to the Win32 API:
http://visualbasic.a...l/aa103002a.htm
http://www.developer...icle.php/610261

Also, may I add that since you are new to VB6, you could switch to VB.NET 2008. Its the version Microsoft will be supporting. Check out http://www.msdn.com to get a free copy of VB 2008 Express. :)

This post has been edited by Louisda16th: 14 January 2008 - 09:11 PM

Was This Post Helpful? 0
  • +
  • -

#15 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Removing records from a database

Posted 14 January 2008 - 11:44 PM

Quote

Possibly, running from a USB stick will require you to use API functions.

Nope. There's no need to use any API Functions because there aren't any for such situations (there's no need for it)

Quote

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?

That entirely depends on the game. Some games install many DLL files and other components in some Windows subdirectory (Usually Windows\system32)

So you'll also need to copy these files along with the ones residing in the game folder into the USB drive.

Since the path of these DLL files is generally hard-coded into the Game code, so there's no way you can play such games directly from the USB drive*. You will have to copy a few files into the Windows directory first.

* There is a way but I don't recommend it. You have to replace the paths from every executable and DLL file that accesses more or more of these files.
But this can be very tricky because sometimes, strings are packed so closely together, there are no additional bytes available to write a new (longer) path.
Again, there is a way out of this one but it's not worth the effort.

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2