|
Post by wexhammer on Jan 6, 2020 5:04:17 GMT -5
I am trying to achieve the possible of being able to save the specific contact data file as another. And then being able to open that saved file and have the listbox in the gui load all that data. So far on default, when the program opens, data is loaded from the default data file. I would like to see how i could achieve this and learn from this as i have been up for hours now trying to get it to work.
I still want the default loading and saving to stay the same.
But have the option to save as a different .dat .bak file, and then be able to open that at any time.
Please help me out! Much appreciate it.
[Initialize] Global hMain, hFind, dupNum, dupo, addName, recordIndex, contactCount Global filterCount, oldStage$ Nomainwin Dim name$(1000), nameSearch$(1000), stage$(20), type$(20) Dim builder$(1000, 13), existInfo$(10,10), hContents$(20)
dupo=0 addName=1
Call loadNames
[GUI]
Stylebits #main, 0,_WS_MAXIMIZEBOX,0,0 WindowWidth = 495 '595 695 795 WindowHeight = 460 UpperLeftX=Int((DisplayWidth-WindowWidth)/2) UpperLeftY=Int((DisplayHeight-WindowHeight)/2)
Statictext #main, "Shortcuts", 10, 10, 60, 20 Listbox #main.contacts, name$(, modifyRecord, 10, 35, 140, 120
Statictext #main, "Name", 195, 10, 50, 20 Textbox #main.name, 250, 10, 160, 25 Statictext #main.3, "Number", 195, 40, 50, 20 Textbox #main.number, 250, 40, 160, 25
button #main.browse, "Open", [open], UL, 320, 80, 50, 20 ' button #main.run, "Open", [open], UL, 435, 10, 40, 20 button #main.run2, "Save File As", [Save], UL, 375, 80, 90, 20
Button #main.new, "&New Entry", addRecord, UL, 10, 360
Listbox #main.namel, dup$(, pick, 490, 10, 190, 395 Button #main.OK, "Save Entry", acceptEntry, UL, 350, 360 Open "Test" For Window As #main Print #main, "Trapclose endProgram"
Print #main.contacts, "SingleClickSelect" Print #main.namel, "SingleClickSelect"
#main.number "!show"
hMain=hwnd(#main)
[Code]
Print #main.contacts, "Setfocus"
[inputLoop] Wait
[acceptEntry] Call acceptEntry handle$ Wait
[addRecord] Call addRecord handle$ Wait
[open]
FileDialog "Select a file", "*.bak*;*.dat*", nameoffile$ if nameoffile$="" then wait
'print #main.name, nameoffile$
Open nameoffile$ For Random As #contacts Len = 710 Field #contacts, 335 As name$, 335 As number$, 20 As stage$, 20 As type$
Open nameoffile$ For Random As #contactsCopy Len = 710 Field #contactsCopy, 335 As name$, 335 As number$, 20 As stage$, 20 As type$
If Eof(#contacts) <> 0 Then Close #contacts Close #contactsCopy Call initializeNames Goto [skipIt] End If
Get #contacts, 1 Put #contactsCopy, 1
contactCount = Val(name$) If contactCount = 0 Then Close #contacts Close #contactsCopy Goto [skipIt] End If For index = 2 To contactCount + 1 Gettrim #contacts, index Put #contactsCopy, index name$(index - 1) = str$(index-1)+") "+name$
Next index Close #contacts Close #contactsCopy
[skipIt]
wait
[Save] filedialog "Save",filePath$+"\*.dat",savefile$ if savefile$="" then wait if instr(savefile$,".")=0 then savefile$=savefile$+".dat"
wait
[endProgram] Call endProgram handle$ Wait
Sub endProgram handle$ close #main end End Sub
Sub loadNames If exists("contact4old.bak") Then Kill "contact4old.bak" End If If exists("contact4.bak") Then Name "contact4.bak" As "contact4old.bak" End If Open "contact4.dat" For Random As #contacts Len = 710 Field #contacts, 335 As name$, 335 As number$, 20 As stage$, 20 As type$
Open "contact4.bak" For Random As #contactsCopy Len = 710 Field #contactsCopy, 335 As name$, 335 As number$, 20 As stage$, 20 As type$
If Eof(#contacts) <> 0 Then Close #contacts Close #contactsCopy Call initializeNames Goto [skipIt] End If
Get #contacts, 1 Put #contactsCopy, 1
contactCount = Val(name$) If contactCount = 0 Then Close #contacts Close #contactsCopy Goto [skipIt] End If For index = 2 To contactCount + 1 Gettrim #contacts, index Put #contactsCopy, index name$(index - 1) = str$(index-1)+") "+name$
Next index Close #contacts Close #contactsCopy [skipIt] End Sub
Sub initializeNames
Open "contact4.dat" For Random As #contacts Len = 710 Field #contacts, 335 As name$, 335 As number$, 20 As stage$, 20 As type$ name$ = "0" Put #contacts, 1 Close #contacts End Sub
[moveOn]
Sub modifyRecord handle$ addName = 0
Print #main.contacts, "Selection? selection$" If selection$ = "" Then [noName]
Print #main.contacts, "SelectionIndex? index" If index = 0 Then [noName] 'This should never need to happen recordIndex=val(selection$) [getContactRecord] getIndex=recordIndex+1 Open "contact4.dat" For Random As #contacts Len = 710 Field #contacts, 335 As name$, 335 As number$, 20 As stage$, 20 As type$ Gettrim #contacts, getIndex Close #contacts Print #main.name, name$ Print #main.number, number$
[noName] End Sub
Sub addRecord handle$ addName = 1 Print #main.contacts, "SelectIndex 0" Print #main.name, "" Print #main.number, ""
' Print #main.stage, "SelectIndex 20" ' Print #main.type, "SelectIndex 20"
Print #main.name, "!SetFocus"
End Sub
Sub acceptEntry handle$ Print #main.name, "!contents? name$"; If name$<>"" Then Print #main.name, "!contents? number$"; Print #main.number, "!contents? number$"; ' Print #main.type, "Selection? type$"; 'Print #main.stage, "Selection? stage$"; If addName = 1 Then contactCount = contactCount + 1 recordIndex = contactCount End If name$(recordIndex) = Str$(recordIndex)+") "+name$ Print #main.contacts, "Reload" date$=Date$() If oldStage$="Opening Mailer" And stage$="Follow Up Call" then mailDate$=Date$() End If [saveContactRecord] Open "contact4.dat" For Random As #contacts Len = 710 Field #contacts, 335 As name$, 335 As number$, 20 As stage$, 20 As type$ Put #contacts, recordIndex + 1 Close #contacts If addName = 0 Then [goBack] Open "contact4.dat" For Random As #contacts Len = 710 Field #contacts, 35 As count$, 675 As fill$ count$ = Str$(contactCount) Put #contacts, 1 Close #contacts addName = 0 [goBack] Print #main.contacts, "SelectIndex ";recordIndex Call addRecord handle$ Else Notice "NO BUSINESS NAME"+Chr$(13)+"The business name must be entered, "_ +Chr$(13)+"before a record can be saved. " End If End Sub
Sub populateList Open "contact4.dat" For random As #contacts3 Len = 710 Field #contacts3, 335 As name$, 335 As number$, 20 As stage$, 20 As type$
Open "contact.dat" For Random As #contacts Len = 710 Field #contacts, 335 As name$, 335 As number$, 20 As stage$, 20 As type$
Get #contacts3, 1 contactCount = Val(name$) Put #contacts, 1
For index2 = 2 To contactCount + 1 Gettrim #contacts3, index2 For column=1 To 4 Select Case column Case 1 builder$(index2, column)=name$ Case 2 builder$(index2, column)=number$
Case 3 builder$(index2, column)=stage$ Case 4 builder$(index2, column)=type$
End Select Next column Next index2 End Sub
Sub saveList For index2 = 2 To contactCount + 1 For column=1 To 4 Select Case column Case 1 name$=builder$(index2, column) Case 2 number$=builder$(index2, column) Case 3 stage$=builder$(index2, column) Case 4 type$=builder$(index2, column)
End Select Next column Put #contacts, index2 Next index2
Close #contacts3 Close #contacts If exists("contact4recoverold.bak") Then Kill "contact4recoverold.bak" End If If exists("contact4recover.bak") Then Name "contact4recover.bak" As "contact4recoverold.bak" End If Name "contact4.dat" As "contact4recover.bak" Name "contact.dat" As "contact4.dat" End Sub
Function exists(a$) Files DefaultDir$, a$, existInfo$() exists=val(existInfo$(0,0)) End Function
Sub removeEntry ' Print #main.filtertype, "selection? filter$" If addName=0 Then Confirm "YOU ARE ABOUT TO DELETE THIS RECORD"+Chr$(13)+" "+Chr$(13)+Chr$(34)_ +name$(recordIndex)+Chr$(34)+Chr$(13)+" "+Chr$(13)+"Do you want to proceed?";delAns$ If Lower$(delAns$)="no" Then [noDelete] WindowWidth=150 WindowHeight=75 StaticText #patience, "PLEASE WAIT...", 10, 15, 100, 25 Open "DELETING...", For Dialog_Modal As #patience
Open "contact4.dat" For Random As #contacts Len = 710 Field #contacts, 335 As name$, 335 As number$, 20 As stage$, 20 As type$
For moveRecords=recordIndex+1 To contactCount Get #contacts, moveRecords+1 Put #contacts, moveRecords Next moveRecords
Get #contacts, 1 name$(Val(name$))="" name$=Str$(Val(name$)-1) Put #contacts, 1
Close #contacts Close #patience Call loadNames Notice "RECORD DELETED"+Chr$(13)+"Names reloaded" Else Notice "NO RECORD SELECTED!"+Chr$(13)+"Please select a record," _ +Chr$(13)+"then select delete from the menu." End If
[noDelete] End Sub
|
|
|
Post by Rod on Jan 6, 2020 6:59:39 GMT -5
So your default database file is called contac4.dat and you have copies as contact4.bak etc. You want to be able to save the file to another name. So just copy the entire file to a new file. Open the new file name for output as #new, open the contact4.dat for input as #old, read in all the data to a string, txt$ = input$(#old, lof(#old)) Then simply write txt to the new file with #new , txt$; Close both job done.
Actually you don't even need the txt$ string Stefan has a better idea. Also the trailing ; is important as it stops the CRLF pair being added to the end of the file.
open "rod.txt" for input as #old open "new.txt" for output as #new #new, input$(#old,lof(#old)); close #old close #new
|
|
|
Post by wexhammer on Jan 6, 2020 11:39:50 GMT -5
Okay, now i understand the copying to a new file while keeping the old. I have modded the code a little more so i can give the file a name on save. Now what i am really stuck on is how would i open that new saved file to view within the program. Basically how the program auto reads contact4.dat on load and displays the data, i am trying to get the program to display the data of the new saved file on opening through a file dialog. I think i'm nearly there but i'm getting a couple of errors and i don't think i'm going down the correct path.
In simpler terms:
1. contact4.dat data has been copied 2. newfilename.dat has been created and the contact4.dat data is now within it (Now i have two copies: 1: contact4.dat and the 2nd: newfilename.dat) 3. Now I can carry on entering data and that data still writes to contact4.dat .... which is perfect 4. (But now the bit i really need help with.) Trying to read the newfilename.dat when i click the open button. So i can edit the contacts/data within the newfilename.
Modification Below:
Global hMain, hFind, dupNum, dupo, addName, recordIndex, contactCount Global filterCount, oldStage$ Nomainwin Dim name$(1000), nameSearch$(1000), stage$(20), type$(20) Dim builder$(1000, 13), existInfo$(10,10), hContents$(20)
dupo=0 addName=1
Call loadNames
[GUI]
Stylebits #main, 0,_WS_MAXIMIZEBOX,0,0 WindowWidth = 495 '595 695 795 WindowHeight = 460 UpperLeftX=Int((DisplayWidth-WindowWidth)/2) UpperLeftY=Int((DisplayHeight-WindowHeight)/2)
Statictext #main, "Shortcuts", 10, 10, 60, 20 Listbox #main.contacts, name$(, modifyRecord, 10, 35, 140, 120
Statictext #main, "Name", 195, 10, 50, 20 Textbox #main.name, 250, 10, 160, 25 Statictext #main.3, "Number", 195, 40, 50, 20 Textbox #main.number, 250, 40, 160, 25
button #main.browse, "Open", [open], UL, 320, 80, 50, 20 ' button #main.run, "Open", [open], UL, 435, 10, 40, 20 button #main.run2, "Save File As", [save], UL, 375, 80, 90, 20
Button #main.new, "&New Entry", addRecord, UL, 10, 360
Listbox #main.namel, dup$(, pick, 490, 10, 190, 395 Button #main.OK, "Save Entry", acceptEntry, UL, 350, 360 Open "Test" For Window As #main Print #main, "Trapclose endProgram"
Print #main.contacts, "SingleClickSelect" Print #main.namel, "SingleClickSelect"
#main.number "!show"
hMain=hwnd(#main)
[Code]
Print #main.contacts, "Setfocus"
[inputLoop] Wait
[acceptEntry] Call acceptEntry handle$ Wait
[addRecord] Call addRecord handle$ Wait
[save]
prompt "Enter the file name to save"; newfilename$
open "contact4.dat" for input as #old open "contact4.bak" for input as #old2 open newfilename$;".dat" for output as #new open newfilename$;".bak" for output as #new1 #new, input$(#old,lof(#old)); #new1, input$(#old2,lof(#old2)); close #old close #old2 close #new close #new1
wait
[open]
filedialog "Open file...","*.dat*;*.bak*",file$ if file$="" then wait
index=len(file$) : length=len(file$)
while mid$(file$,index,1)<>"\" index=index-1 wend
lpszFile$=right$(file$,length-index)
lpszDirectory$ = left$(file$,index)
lpszResult$ = space$(255) + chr$(0)
calldll #shell32, "FindExecutableA",_ lpszFile$ as ptr,lpszDirectory$ as ptr,lpszResult$ as ptr,_ result as long
associatedFile$ = trim$(lpszResult$) notice, lpszFile$
call loadNames wait
[endProgram] Call endProgram handle$ Wait
Sub endProgram handle$ close #main end End Sub
Sub loadNames If exists("contact4old.bak") Then Kill "contact4old.bak" End If If exists("contact4.bak") Then Name "contact4.bak" As "contact4old.bak" End If ' if lpszFile$ = "contact4.dat" then
Open "contact4.dat" For Random As #contacts Len = 710 Field #contacts, 335 As name$, 335 As number$, 20 As stage$, 20 As type$
Open "contact4.bak" For Random As #contactsCopy Len = 710 Field #contactsCopy, 335 As name$, 335 As number$, 20 As stage$, 20 As type$
If Eof(#contacts) <> 0 Then Close #contacts Close #contactsCopy Call initializeNames Goto [skipIt] End If
Get #contacts, 1 Put #contactsCopy, 1
contactCount = Val(name$) If contactCount = 0 Then Close #contacts Close #contactsCopy Goto [skipIt] End If For index = 2 To contactCount + 1 Gettrim #contacts, index Put #contactsCopy, index name$(index - 1) = str$(index-1)+") "+name$
Next index Close #contacts Close #contactsCopy [skipIt] End Sub
Sub initializeNames
Open "contact4.dat" For Random As #contacts Len = 710 Field #contacts, 335 As name$, 335 As number$, 20 As stage$, 20 As type$ name$ = "0" Put #contacts, 1 Close #contacts End Sub
[moveOn]
Sub modifyRecord handle$ addName = 0
Print #main.contacts, "Selection? selection$" If selection$ = "" Then [noName]
Print #main.contacts, "SelectionIndex? index" If index = 0 Then [noName] 'This should never need to happen recordIndex=val(selection$) [getContactRecord] getIndex=recordIndex+1 Open "contact4.dat" For Random As #contacts Len = 710 Field #contacts, 335 As name$, 335 As number$, 20 As stage$, 20 As type$ Gettrim #contacts, getIndex Close #contacts Print #main.name, name$ Print #main.number, number$
[noName] End Sub
Sub addRecord handle$ addName = 1 Print #main.contacts, "SelectIndex 0" Print #main.name, "" Print #main.number, ""
' Print #main.stage, "SelectIndex 20" ' Print #main.type, "SelectIndex 20"
Print #main.name, "!SetFocus"
End Sub
Sub acceptEntry handle$ Print #main.name, "!contents? name$"; If name$<>"" Then Print #main.name, "!contents? number$"; Print #main.number, "!contents? number$"; ' Print #main.type, "Selection? type$"; 'Print #main.stage, "Selection? stage$"; If addName = 1 Then contactCount = contactCount + 1 recordIndex = contactCount End If name$(recordIndex) = Str$(recordIndex)+") "+name$ Print #main.contacts, "Reload" date$=Date$() If oldStage$="Opening Mailer" And stage$="Follow Up Call" then mailDate$=Date$() End If [saveContactRecord] Open "contact4.dat" For Random As #contacts Len = 710 Field #contacts, 335 As name$, 335 As number$, 20 As stage$, 20 As type$ Put #contacts, recordIndex + 1 Close #contacts If addName = 0 Then [goBack] Open "contact4.dat" For Random As #contacts Len = 710 Field #contacts, 35 As count$, 675 As fill$ count$ = Str$(contactCount) Put #contacts, 1 Close #contacts addName = 0 [goBack] Print #main.contacts, "SelectIndex ";recordIndex Call addRecord handle$ Else Notice "NO BUSINESS NAME"+Chr$(13)+"The business name must be entered, "_ +Chr$(13)+"before a record can be saved. " End If End Sub
Sub populateList Open "contact4.dat" For random As #contacts3 Len = 710 Field #contacts3, 335 As name$, 335 As number$, 20 As stage$, 20 As type$
Open "contact.dat" For Random As #contacts Len = 710 Field #contacts, 335 As name$, 335 As number$, 20 As stage$, 20 As type$
Get #contacts3, 1 contactCount = Val(name$) Put #contacts, 1
For index2 = 2 To contactCount + 1 Gettrim #contacts3, index2 For column=1 To 4 Select Case column Case 1 builder$(index2, column)=name$ Case 2 builder$(index2, column)=number$
Case 3 builder$(index2, column)=stage$ Case 4 builder$(index2, column)=type$
End Select Next column Next index2 End Sub
Sub saveList For index2 = 2 To contactCount + 1 For column=1 To 4 Select Case column Case 1 name$=builder$(index2, column) Case 2 number$=builder$(index2, column) Case 3 stage$=builder$(index2, column) Case 4 type$=builder$(index2, column)
End Select Next column Put #contacts, index2 Next index2
Close #contacts3 Close #contacts If exists("contact4recoverold.bak") Then Kill "contact4recoverold.bak" End If If exists("contact4recover.bak") Then Name "contact4recover.bak" As "contact4recoverold.bak" End If Name "contact4.dat" As "contact4recover.bak" Name "contact.dat" As "contact4.dat" End Sub
Function exists(a$) Files DefaultDir$, a$, existInfo$() exists=val(existInfo$(0,0)) End Function
Sub removeEntry ' Print #main.filtertype, "selection? filter$" If addName=0 Then Confirm "YOU ARE ABOUT TO DELETE THIS RECORD"+Chr$(13)+" "+Chr$(13)+Chr$(34)_ +name$(recordIndex)+Chr$(34)+Chr$(13)+" "+Chr$(13)+"Do you want to proceed?";delAns$ If Lower$(delAns$)="no" Then [noDelete] WindowWidth=150 WindowHeight=75 StaticText #patience, "PLEASE WAIT...", 10, 15, 100, 25 Open "DELETING...", For Dialog_Modal As #patience
Open "contact4.dat" For Random As #contacts Len = 710 Field #contacts, 335 As name$, 335 As number$, 20 As stage$, 20 As type$
For moveRecords=recordIndex+1 To contactCount Get #contacts, moveRecords+1 Put #contacts, moveRecords Next moveRecords
Get #contacts, 1 name$(Val(name$))="" name$=Str$(Val(name$)-1) Put #contacts, 1
Close #contacts Close #patience Call loadNames Notice "RECORD DELETED"+Chr$(13)+"Names reloaded" Else Notice "NO RECORD SELECTED!"+Chr$(13)+"Please select a record," _ +Chr$(13)+"then select delete from the menu." End If
[noDelete] End Sub
|
|
|
Post by Rod on Jan 6, 2020 14:21:28 GMT -5
Not sure why we have a problem. The .dat can have any name and the records can be different. Just open whichever one you want when you want to use it. You can even have two open at once provided you remember to put and get to both.
Neither am I sure why you are mixing in a variety of file types in the [open] code block. You are going to have to handle .dat specially but I am not sure why you wish to mix in .txt .xls .this .that in a database program.
|
|
|
Post by wexhammer on Jan 6, 2020 16:20:36 GMT -5
I am not mixing extra file names in. That was for a link option to spread sheets etc...
What i cant get my head around is, how would i open the newfilename.dat or whatever i saved it as, to load the data in the program the same way as the default contact4.dat is loaded.
So in simpler terms: I wanna edit and view some records in the new saved file. Then when i want to, i can switch back to the contact.dat file, to carry on inputting records into that.
Then if i want to view the other file, i can put records into that, so how ever many files i have i can view any one of them upon opening them through the dialog and the listbox and textfields will display the data like contact4.dat does.
How would i open it to read and edit within my code?
|
|
|
Post by Chris Iverson on Jan 6, 2020 16:50:09 GMT -5
Why are you doing a FindExecutable() in the open handler, seemingly looking for the data file you want?
Why can you not just take the filename returned from the filedialog, and open it?
open file$ for input as #file
Then do your data processing on it?
|
|
|
Post by Rod on Jan 7, 2020 3:50:53 GMT -5
Yes, because the data structure is the same you’re files statement will be the same so the only thing needing changed is the file name. Perhaps move the link code to a new handler, perhaps add an import/view button for the other file handling. But you are not going to copy records from one to the other without some more code. So it’s more than just opening the two .dat files. Depending on what your process is you will probably want some sort of flag on each record to copy then a copy routine.
|
|
|
Post by wexhammer on Jan 7, 2020 3:52:47 GMT -5
I am getting an error with this:
[open]
filedialog "Open file...","*.dat*;*.bak*",file$ if file$="" then wait
index=len(file$) : length=len(file$)
while mid$(file$,index,1)<>"\" index=index-1 wend
'Open file$ For Random As #file Len = 710 ' Field #file, 335 As name$, 335 As number$, 20 As stage$, 20 As type$
getIndex=recordIndex+1 Open file$ For Random As #file Len = 710 Field #file, 335 As name$, 335 As number$, 20 As stage$, 20 As type$ Gettrim #file, getIndex Close #file ' close #contacts Print #main.name, name$ Print #main.number, number$
If Eof(#file) <> 0 Then Close #file
Call initializeNames Goto [skipIt] End If
Get #file, 1 ' Put #contactsCopy, 1
contactCount = Val(name$) If contactCount = 0 Then Close #file ' Close #contactsCopy Goto [skipIt] End If For index = 2 To contactCount + 1 Gettrim #file, index ' Put #contactsCopy, index name$(index - 1) = str$(index-1)+") "+name$
Next index Close #file ' Close #contactsCopy
wait
|
|
|
Post by Rod on Jan 7, 2020 5:44:57 GMT -5
The code is pretty much a mix up, I can't tell what is wrong unless you give us the exact error. But look at this, I open two .dat files and copy records from one to the other. If you are going to be moving records from one .dat to another you are going to have to do something like this. You would need a flag in each record to flag whether it is to be copied or not.
'create a test file file$="test.dat" Open file$ For Random As #main Len = 710 Field #main, 335 As name$, 335 As number$, 20 As stage$, 20 As type$ name$="2" put #main,1 name$="Rod" put #main,2 name$="Wrexhammer" put #main,3 close #main
[open] getIndex=1
'get user to open main file filedialog "Open Original file...","*.dat",file$ if file$="" then wait Open file$ For Random As #main Len = 710 Field #main, 335 As name$, 335 As number$, 20 As stage$, 20 As type$ if lof(#main)=0 then wait Gettrim #main, getIndex contactCountMain = val(name$)
'get user to open new file filedialog "Open Copy file...","*.dat",file$ if file$="" then wait Open file$ For Random As #new Len = 710 Field #new, 335 As name$, 335 As number$, 20 As stage$, 20 As type$ if lof(#new)=0 then name$="0" : put #new, 1 Gettrim #new, getIndex contactCountNew = val(name$)
'append all the records from the main file to the second file 'in reality you would probably only copy flagged records if contactCountNew=0 then indexNew=2 else indexNew=contactCountNew+1 For index = 2 To contactCountMain + 1 Gettrim #main, index put #new, indexNew indexNew=indexNew+1 Next index 'save the record count name$=str$(indexNew-2) put #new, 1
Close #main close #new
wait
|
|
|
Post by Rod on Jan 7, 2020 5:48:40 GMT -5
Perhaps I should add that while I use the same field statement and it works it does limit what you can do. If you are doing more than simply swapping records you should have a unique field statement for each file and swap the info before the put so newname$=name$ put #new index
|
|
|
Post by wexhammer on Jan 7, 2020 8:07:51 GMT -5
Thank you for the help so far, but i am not trying to achieve copying records from one to another.
The program below when run has a contact4.dat file on default, this means i can crack on straight away adding in data clicking save and the names will load in the list box and so on..
Then if i decide i want to save that contact4.dat as something else because i want to start a new dat file for new data i can.
Were i am stuck, is if i want to view the data from a dat file i have saved, how would i get the contents to load in the same way as the contact4.dat file does when i open it.
Being able to open any previous saved dat files if i want to, the same way as the program auto opens the contact4.dat and reads the data so i can view in the list box etc.... is a must
Global hMain, hFind, dupNum, dupo, addName, recordIndex, contactCount Global filterCount, oldStage$ Nomainwin Dim name$(1000), nameSearch$(1000), stage$(20), type$(20) Dim builder$(1000, 13), existInfo$(10,10), hContents$(20)
dupo=0 addName=1
Call loadNames
[GUI]
Stylebits #main, 0,_WS_MAXIMIZEBOX,0,0 WindowWidth = 495 '595 695 795 WindowHeight = 460 UpperLeftX=Int((DisplayWidth-WindowWidth)/2) UpperLeftY=Int((DisplayHeight-WindowHeight)/2)
Statictext #main, "Shortcuts", 10, 10, 60, 20 Listbox #main.contacts, name$(, modifyRecord, 10, 35, 140, 120
Statictext #main, "Name", 195, 10, 50, 20 Textbox #main.name, 250, 10, 160, 25 Statictext #main.3, "Number", 195, 40, 50, 20 Textbox #main.number, 250, 40, 160, 25
button #main.browse, "Open", [open], UL, 320, 80, 50, 20 ' button #main.run, "Open", [open], UL, 435, 10, 40, 20 button #main.run2, "Save File As", [save], UL, 375, 80, 90, 20
Button #main.new, "&New Entry", addRecord, UL, 10, 360
Button #main.remove, "&Remove Entry", removeEntry , UL, 190, 360
Listbox #main.namel, dup$(, pick, 490, 10, 190, 395 Button #main.OK, "Save Entry", acceptEntry, UL, 350, 360 Open "Test" For Window As #main Print #main, "Trapclose endProgram"
Print #main.contacts, "SingleClickSelect" Print #main.namel, "SingleClickSelect"
#main.number "!show"
hMain=hwnd(#main)
[Code]
Print #main.contacts, "Setfocus"
[inputLoop] Wait
[acceptEntry] Call acceptEntry handle$ Wait
[addRecord] Call addRecord handle$ Wait
[save]
prompt "Enter the file name to save"; newfilename$
open "contact4.dat" for input as #old open "contact4.bak" for input as #old2 open newfilename$;".dat" for output as #new open newfilename$;".bak" for output as #new1 #new, input$(#old,lof(#old)); #new1, input$(#old2,lof(#old2)); close #old close #old2 close #new close #new1
wait
'[open] ''create a test file ' file$="test.dat" ' Open file$ For Random As #main Len = 710 'Field #main, 335 As name$, 335 As number$, 20 As stage$, 20 As type$ ' name$="2" ' put #main,1 ' name$="Rod" ' put #main,2 ' name$="Wrexhammer" ' put #main,3 ' close #main
[open] getIndex=1
'get user to open main file filedialog "Open Original file...","*.dat",file$ if file$="" then wait Open file$ For Random As #op Len = 710 Field #op, 335 As name$, 335 As number$, 20 As stage$, 20 As type$ if lof(#op)=0 then wait Gettrim #op, getIndex contactCountMain = val(name$)
'get user to open new file filedialog "Open Copy file...","*.dat",file$ if file$="" then wait Open file$ For Random As #new Len = 710 Field #new, 335 As name$, 335 As number$, 20 As stage$, 20 As type$ if lof(#new)=0 then name$="0" : put #new, 1 Call initializeNames Gettrim #new, getIndex contactCountNew = val(name$)
'append all the records from the main file to the second file 'in reality you would probably only copy flagged records if contactCountNew=0 then indexNew=2 else indexNew=contactCountNew+1 For index = 2 To contactCountMain + 1 Gettrim #op, index put #new, indexNew indexNew=indexNew+1 Next index 'save the record count name$=str$(indexNew-2) put #new, 1
Close #op close #new Print #main.name, name$ Print #main.number, number$ wait
[endProgram] Call endProgram handle$ Wait
Sub endProgram handle$ close #main end End Sub
Sub loadNames If exists("contact4old.bak") Then Kill "contact4old.bak" End If If exists("contact4.bak") Then Name "contact4.bak" As "contact4old.bak" End If
Open "contact4.dat" For Random As #contacts Len = 710 Field #contacts, 335 As name$, 335 As number$, 20 As stage$, 20 As type$
Open "contact4.bak" For Random As #contactsCopy Len = 710 Field #contactsCopy, 335 As name$, 335 As number$, 20 As stage$, 20 As type$
If Eof(#contacts) <> 0 Then Close #contacts Close #contactsCopy Call initializeNames Goto [skipIt] End If
Get #contacts, 1 Put #contactsCopy, 1
contactCount = Val(name$) If contactCount = 0 Then Close #contacts Close #contactsCopy Goto [skipIt] End If For index = 2 To contactCount + 1 Gettrim #contacts, index Put #contactsCopy, index name$(index - 1) = str$(index-1)+") "+name$
Next index Close #contacts Close #contactsCopy [skipIt] End Sub
Sub initializeNames
Open "contact4.dat" For Random As #contacts Len = 710 Field #contacts, 335 As name$, 335 As number$, 20 As stage$, 20 As type$ name$ = "0" Put #contacts, 1 Close #contacts End Sub
[moveOn]
Sub modifyRecord handle$ addName = 0
Print #main.contacts, "Selection? selection$" If selection$ = "" Then [noName]
Print #main.contacts, "SelectionIndex? index" If index = 0 Then [noName] 'This should never need to happen recordIndex=val(selection$) [getContactRecord] getIndex=recordIndex+1 Open "contact4.dat" For Random As #contacts Len = 710 Field #contacts, 335 As name$, 335 As number$, 20 As stage$, 20 As type$ Gettrim #contacts, getIndex Close #contacts Print #main.name, name$ Print #main.number, number$
[noName] End Sub
Sub addRecord handle$ addName = 1 Print #main.contacts, "SelectIndex 0" Print #main.name, "" Print #main.number, ""
' Print #main.stage, "SelectIndex 20" ' Print #main.type, "SelectIndex 20"
Print #main.name, "!SetFocus"
End Sub
Sub acceptEntry handle$ Print #main.name, "!contents? name$"; If name$<>"" Then Print #main.name, "!contents? number$"; Print #main.number, "!contents? number$"; ' Print #main.type, "Selection? type$"; 'Print #main.stage, "Selection? stage$"; If addName = 1 Then contactCount = contactCount + 1 recordIndex = contactCount End If name$(recordIndex) = Str$(recordIndex)+") "+name$ Print #main.contacts, "Reload" date$=Date$() If oldStage$="Opening Mailer" And stage$="Follow Up Call" then mailDate$=Date$() End If [saveContactRecord] Open "contact4.dat" For Random As #contacts Len = 710 Field #contacts, 335 As name$, 335 As number$, 20 As stage$, 20 As type$ Put #contacts, recordIndex + 1 Close #contacts If addName = 0 Then [goBack] Open "contact4.dat" For Random As #contacts Len = 710 Field #contacts, 35 As count$, 675 As fill$ count$ = Str$(contactCount) Put #contacts, 1 Close #contacts addName = 0 [goBack] Print #main.contacts, "SelectIndex ";recordIndex Call addRecord handle$ Else Notice "NO BUSINESS NAME"+Chr$(13)+"The business name must be entered, "_ +Chr$(13)+"before a record can be saved. " End If End Sub
Sub populateList Open "contact4.dat" For random As #contacts3 Len = 710 Field #contacts3, 335 As name$, 335 As number$, 20 As stage$, 20 As type$
Open "contact.dat" For Random As #contacts Len = 710 Field #contacts, 335 As name$, 335 As number$, 20 As stage$, 20 As type$
Get #contacts3, 1 contactCount = Val(name$) Put #contacts, 1
For index2 = 2 To contactCount + 1 Gettrim #contacts3, index2 For column=1 To 4 Select Case column Case 1 builder$(index2, column)=name$ Case 2 builder$(index2, column)=number$
Case 3 builder$(index2, column)=stage$ Case 4 builder$(index2, column)=type$
End Select Next column Next index2 End Sub
Sub saveList For index2 = 2 To contactCount + 1 For column=1 To 4 Select Case column Case 1 name$=builder$(index2, column) Case 2 number$=builder$(index2, column) Case 3 stage$=builder$(index2, column) Case 4 type$=builder$(index2, column)
End Select Next column Put #contacts, index2 Next index2
Close #contacts3 Close #contacts If exists("contact4recoverold.bak") Then Kill "contact4recoverold.bak" End If If exists("contact4recover.bak") Then Name "contact4recover.bak" As "contact4recoverold.bak" End If Name "contact4.dat" As "contact4recover.bak" Name "contact.dat" As "contact4.dat" End Sub
Function exists(a$) Files DefaultDir$, a$, existInfo$() exists=val(existInfo$(0,0)) End Function
Sub removeEntry handle$ ' Print #main.filtertype, "selection? filter$" If addName=0 Then Confirm "YOU ARE ABOUT TO DELETE THIS RECORD"+Chr$(13)+" "+Chr$(13)+Chr$(34)_ +name$(recordIndex)+Chr$(34)+Chr$(13)+" "+Chr$(13)+"Do you want to proceed?";delAns$ If Lower$(delAns$)="no" Then [noDelete] WindowWidth=150 WindowHeight=75 StaticText #patience, "PLEASE WAIT...", 10, 15, 100, 25 Open "DELETING...", For Dialog_Modal As #patience
Open "contact4.dat" For Random As #contacts Len = 710 Field #contacts, 335 As name$, 335 As number$, 20 As stage$, 20 As type$
For moveRecords=recordIndex+1 To contactCount Get #contacts, moveRecords+1 Put #contacts, moveRecords Next moveRecords
Get #contacts, 1 name$(Val(name$))="" name$=Str$(Val(name$)-1) Put #contacts, 1
Close #contacts Close #patience Call loadNames Notice "RECORD DELETED"+Chr$(13)+"Names reloaded" Else Notice "NO RECORD SELECTED!"+Chr$(13)+"Please select a record," _ +Chr$(13)+"then select delete from the menu." End If
[noDelete] End Sub
|
|
|
Post by Rod on Jan 7, 2020 9:10:46 GMT -5
Well perhaps we need to go back to basics. The first thing you need to stop doing is opening a specific .dat in all your subs. You should open the .dat at the start of the program. Leave it open, all your subs don't need to open the .dat, just use it. Call it #current
Only close #current when you want to change files or are completely finished.
Now you can put your [open] branch in a sub. Pass it the filename you want opened. Now you can open any file as #current. Because your other subs don't care they can all be called to act on #current So you would call your initialisation and loaddata subs a second time to redim the arrays and load the contents of the new file.
Any clearer?
|
|
|
Post by wexhammer on Jan 7, 2020 10:04:49 GMT -5
Okay, so i have done just that, and have made sure the open file is the first thing the program does on open. Now nothing loads...
Global hMain, hFind, dupNum, dupo, addName, recordIndex, contactCount Global filterCount, oldStage$ Nomainwin Dim name$(1000), nameSearch$(1000), stage$(20), type$(20) Dim builder$(1000, 13), existInfo$(10,10), hContents$(20)
dupo=0 addName=1
' Call loadNames
[GUI]
Stylebits #main, 0,_WS_MAXIMIZEBOX,0,0 WindowWidth = 495 '595 695 795 WindowHeight = 460 UpperLeftX=Int((DisplayWidth-WindowWidth)/2) UpperLeftY=Int((DisplayHeight-WindowHeight)/2)
Statictext #main, "Shortcuts", 10, 10, 60, 20 Listbox #main.contacts, name$(, modifyRecord, 10, 35, 140, 120
Statictext #main, "Name", 195, 10, 50, 20 Textbox #main.name, 250, 10, 160, 25 Statictext #main.3, "Number", 195, 40, 50, 20 Textbox #main.number, 250, 40, 160, 25
button #main.browse, "Open", [open], UL, 320, 80, 50, 20 ' button #main.run, "Open", [open], UL, 435, 10, 40, 20 button #main.run2, "Save File As", [save], UL, 375, 80, 90, 20
Button #main.new, "&New Entry", addRecord, UL, 10, 360
Button #main.remove, "&Remove Entry", removeEntry , UL, 190, 360
Listbox #main.namel, dup$(, pick, 490, 10, 190, 395 Button #main.OK, "Save Entry", acceptEntry, UL, 350, 360 Open "Test" For Window As #main Print #main, "Trapclose endProgram"
Print #main.contacts, "SingleClickSelect" Print #main.namel, "SingleClickSelect"
#main.number "!show"
hMain=hwnd(#main)
[Code]
Print #main.contacts, "Setfocus"
[inputLoop] Wait
[acceptEntry] Call acceptEntry handle$ Wait
[addRecord] Call addRecord handle$ Wait
[save]
prompt "Enter the file name to save"; newfilename$
open file$ for input as #old open file$ for input as #old2 open newfilename$;".dat" for output as #new open newfilename$;".bak" for output as #new1 #new, input$(#old,lof(#old)); #new1, input$(#old2,lof(#old2)); close #old close #old2 close #new close #new1
wait
'[open] ''create a test file ' file$="test.dat" ' Open file$ For Random As #main Len = 710 'Field #main, 335 As name$, 335 As number$, 20 As stage$, 20 As type$ ' name$="2" ' put #main,1 ' name$="Rod" ' put #main,2 ' name$="Wrexhammer" ' put #main,3 ' close #main
[open] call loadNames wait
[endProgram] Call endProgram handle$ Wait
Sub endProgram handle$ close #main end End Sub
Sub loadNames
'getIndex=1
'get user to open main file filedialog "Open Original file...","*.dat",file$ if file$="" then wait
' Kill "contact4old.bak" ' End If ' If exists("contact4.bak") Then ' Name "contact4.bak" As "contact4old.bak" ' End If
Open file$ For Random As #current Len = 710 Field #current, 335 As name$, 335 As number$, 20 As stage$, 20 As type$
Open file$ For Random As #currentCopy Len = 710 Field #currentCopy, 335 As name$, 335 As number$, 20 As stage$, 20 As type$
If Eof(#current) <> 0 Then Close #current Close #currentCopy Call initializeNames Goto [skipIt] End If
Get #current, 1 Put #currentCopy, 1
contactCount = Val(name$) If contactCount = 0 Then Close #current Close #currentCopy Goto [skipIt] End If For index = 2 To contactCount + 1 Gettrim #current, index Put #currentCopy, index name$(index - 1) = str$(index-1)+") "+name$
Next index Close #current Close #currentCopy [skipIt] End Sub
Sub initializeNames
Open file$ For Random As #current Len = 710 Field #current, 335 As name$, 335 As number$, 20 As stage$, 20 As type$ name$ = "0" Put #current, 1 Close #current End Sub
[moveOn]
Sub modifyRecord handle$ addName = 0
Print #main.contacts, "Selection? selection$" If selection$ = "" Then [noName]
Print #main.contacts, "SelectionIndex? index" If index = 0 Then [noName] 'This should never need to happen recordIndex=val(selection$) [getContactRecord] getIndex=recordIndex+1 Open file$ For Random As #current Len = 710 Field #current, 335 As name$, 335 As number$, 20 As stage$, 20 As type$ Gettrim #current, getIndex Close #current Print #main.name, name$ Print #main.number, number$
[noName] End Sub
Sub addRecord handle$ addName = 1 Print #main.contacts, "SelectIndex 0" Print #main.name, "" Print #main.number, ""
' Print #main.stage, "SelectIndex 20" ' Print #main.type, "SelectIndex 20"
Print #main.name, "!SetFocus"
End Sub
Sub acceptEntry handle$ Print #main.name, "!contents? name$"; If name$<>"" Then Print #main.name, "!contents? number$"; Print #main.number, "!contents? number$"; ' Print #main.type, "Selection? type$"; 'Print #main.stage, "Selection? stage$"; If addName = 1 Then contactCount = contactCount + 1 recordIndex = contactCount End If name$(recordIndex) = Str$(recordIndex)+") "+name$ Print #main.contacts, "Reload" date$=Date$() If oldStage$="Opening Mailer" And stage$="Follow Up Call" then mailDate$=Date$() End If [saveContactRecord] Open file$ For Random As #current Len = 710 Field #current, 335 As name$, 335 As number$, 20 As stage$, 20 As type$ Put #current, recordIndex + 1 Close #current If addName = 0 Then [goBack] Open file$ For Random As #current Len = 710 Field #current, 35 As count$, 675 As fill$ count$ = Str$(contactCount) Put #current, 1 Close #current addName = 0 [goBack] Print #main.contacts, "SelectIndex ";recordIndex Call addRecord handle$ Else Notice "NO BUSINESS NAME"+Chr$(13)+"The business name must be entered, "_ +Chr$(13)+"before a record can be saved. " End If End Sub
Sub populateList Open file$ For random As #current3 Len = 710 Field #current3, 335 As name$, 335 As number$, 20 As stage$, 20 As type$
Open file$ For Random As #current Len = 710 Field #current, 335 As name$, 335 As number$, 20 As stage$, 20 As type$
Get #current3, 1 contactCount = Val(name$) Put #current, 1
For index2 = 2 To contactCount + 1 Gettrim #current3, index2 For column=1 To 4 Select Case column Case 1 builder$(index2, column)=name$ Case 2 builder$(index2, column)=number$
Case 3 builder$(index2, column)=stage$ Case 4 builder$(index2, column)=type$
End Select Next column Next index2 End Sub
Sub saveList For index2 = 2 To contactCount + 1 For column=1 To 4 Select Case column Case 1 name$=builder$(index2, column) Case 2 number$=builder$(index2, column) Case 3 stage$=builder$(index2, column) Case 4 type$=builder$(index2, column)
End Select Next column Put #current, index2 Next index2
Close #current3 Close #current ' If exists("contact4recoverold.bak") Then ' Kill "contact4recoverold.bak" ' End If ' If exists("contact4recover.bak") Then ' Name "contact4recover.bak" As "contact4recoverold.bak" ' End If ' Name "contact4.dat" As "contact4recover.bak" ' Name "contact.dat" As "contact4.dat" End Sub
Function exists(a$) Files DefaultDir$, a$, existInfo$() exists=val(existInfo$(0,0)) End Function
Sub removeEntry handle$ ' Print #main.filtertype, "selection? filter$" If addName=0 Then Confirm "YOU ARE ABOUT TO DELETE THIS RECORD"+Chr$(13)+" "+Chr$(13)+Chr$(34)_ +name$(recordIndex)+Chr$(34)+Chr$(13)+" "+Chr$(13)+"Do you want to proceed?";delAns$ If Lower$(delAns$)="no" Then [noDelete] WindowWidth=150 WindowHeight=75 StaticText #patience, "PLEASE WAIT...", 10, 15, 100, 25 Open "DELETING...", For Dialog_Modal As #patience
Open file$ For Random As #current Len = 710 Field #current, 335 As name$, 335 As number$, 20 As stage$, 20 As type$
For moveRecords=recordIndex+1 To contactCount Get #current, moveRecords+1 Put #current, moveRecords Next moveRecords
Get #current, 1 name$(Val(name$))="" name$=Str$(Val(name$)-1) Put #current, 1
Close #current Close #patience Call loadNames Notice "RECORD DELETED"+Chr$(13)+"Names reloaded" Else Notice "NO RECORD SELECTED!"+Chr$(13)+"Please select a record," _ +Chr$(13)+"then select delete from the menu." End If
[noDelete] End Sub
|
|
|
Post by Rod on Jan 7, 2020 10:58:19 GMT -5
Well there is a lot of extraneous stuff in there. You are still closing the file, leave it open so your subs can access it. I have changed two subs to show what I mean. You are using loadnames to open the database file. You should then call populatelist to set up the listbox as you did originally. Only this time you don't open and close the database file you just use its handle in the sub, #current. The file is global so subs can see it.
Remove all your open and close commands from all subs. Only close #current if you are changing file or ending.
EDIT it was name$ you were using for the listbox new debugged code. The builders$( array would need populating to.
global contactCount,openFlag dim name$(100)
[GUI]
Stylebits #main, 0,_WS_MAXIMIZEBOX,0,0 WindowWidth = 495 '595 695 795 WindowHeight = 460 UpperLeftX=Int((DisplayWidth-WindowWidth)/2) UpperLeftY=Int((DisplayHeight-WindowHeight)/2) Statictext #main, "Shortcuts", 10, 10, 60, 20 Listbox #main.contacts, name$(, modifyRecord, 10, 35, 140, 120 Statictext #main, "Name", 195, 10, 50, 20 Textbox #main.name, 250, 10, 160, 25 Statictext #main.3, "Number", 195, 40, 50, 20 Textbox #main.number, 250, 40, 160, 25 button #main.browse, "Open", loadNames, UL, 320, 80, 50, 20 button #main.run2, "Save File As", saveit, UL, 375, 80, 90, 20 Button #main.new, "&New Entry", addRecord, UL, 10, 360 Button #main.remove, "&Remove Entry", removeEntry , UL, 190, 360 Listbox #main.namel, dup$(, pick, 490, 10, 190, 395 Button #main.OK, "Save Entry", acceptEntry, UL, 350, 360 Open "Test" For Window As #main #main "Trapclose endProgram" #main.contacts "SingleClickSelect" #main.namel "SingleClickSelect" #main.contacts "Setfocus" Wait
Sub endProgram handle$ if openFlag then close #current close #main end End Sub
Sub loadNames handle$ if openFlag then close #current
'get user to open main file filedialog "Open Original file...","*.dat",file$ Open file$ For Random As #current Len = 710 Field #current, 335 As name$, 335 As number$, 20 As stage$, 20 As type$ if lof(#current)=0 then name$ = "0" put #current, 1 else get #current, 1 contactCount = Val(name$) end if openFlag=1 call populateList end Sub
Sub populateList for index = 2 to contactCount + 1 gettrim #current, index name$(index)=name$ next index #main.contacts "reload" End Sub
|
|
|
Post by wexhammer on Jan 7, 2020 11:55:56 GMT -5
Ok thanks, its something to work on and i will let you know if any other problems arise.
|
|