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!
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 = ""
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
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.
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
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.
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
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: 9 Jan, 2008 - 06:52 AM
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
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
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 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.