 |
|
 |
stickleprojects Level: Moderator

 Registered: 09-09-2002 Posts: 891
|
Re: How can i load combobox from ".txt" document?
Hi Ragess...
Will answer these in turn:
1. To open a .TXT document in VBA.
Dim fin as integer ' create a variable to refer to the file
fin=freefile ' to open a document you share "handles" to files in windows.. so get a free "handle"
open "mytextfile.txt" for input as fin ' open the file for reading
dim aline as string
while not eof(fin) ' read until "end of file" of file referred to by fin (in this case, mytextfile.txt)
line input #fin,aline ' read a line from the file "fin" (mytextfile.txt)
' my code goes here ****1
wend ' end while, ie. loop round to the While not eof statement
2. Process the line "aline" and read what you want. For example, to display each line in the combo, replace code ****1 with:
me.combobox1.additem aline
3. To create a directory/folder use command mkdir. As in:
mkdir "myfoldername"
4. To create a folder for each item in a listbox:
dim intListIndex as integer
for intListindex = 0 to me.combobox1.listcount-1
mkdir me.combobox1.list(intListIndex)
next intListindex
5. To open a word document, there are a number of options, however the easiest is to use the Shell command. ie.
Shell "myworddoc.doc"
This will open word and show your document.
6. A feature of word is to save a document as a "template". Then whever it is opened, and the user clicks "save", the user has to specify a filename. Therefore, you save your pre-typed document as a "document template" (extension is usually .Dot, rather than .Doc) and Shell out to it.
Of course, there are a load of added complexity that you could use to achieve the same goals, however, these answers were the simplest (and quickest to do) that I could think of.
Hope they help,
Kieron
____________________________
Build it better, faster, quicker, easier.. then fix it (non-offical MS mission statement)
|
|
21-03-2004 at 04:05 PM |
|
|
Goran Level: Moderator
 Registered: 16-05-2002 Posts: 1681
|
Re: How can i load combobox from ".txt" document?
quote: Trouble was not there BUT is it good way?
Yes, it is good way. But, if data from your textbox isnt changed from outside, then there is no need for loading the SAME data again. So, maybe you can put your code in Form_Load event so it will be executed only once. If user from your application changes data in your text file, then you can have module-level boolean variable (boChanged) and when changes occur, you set its value to True, and then when population combo you first check its value:
Private Sub Command1_Click()
If Not boChanged then exit sub ' text file isnt changed so we are exting
' changes occured in text file,
' so you need to load it to combobox again
Combo1.Clear
' Read data and populate combo
Exit Sub |
Or, if you dont want to make your code harder to read, then just use your Clear method as you do. Another thing is that you dont need to load all data to combo when you add item to text file. You can only add new item to combobox, because all other items are the same. This are all different programming tehniques, and program execution speed will depend on which one you use.
quote: Another thing is it made directories everytime. So i got many directories with the same name(there was error). What would happen if i've files in these directories?
As for making directories, you can check if directory already exist. If doesnt exist, then you create one.
| If Dir("Mydir", vbDirectory) = "" Then MkDir "MyDir" |
____________________________
If you find the answer helpful, please mark this topic as solved.
|
|
29-03-2004 at 05:08 PM |
|
|
Goran Level: Moderator
 Registered: 16-05-2002 Posts: 1681
|
Re: How can i load combobox from ".txt" document?
Well, as stckleprojects wrote, loop through all combo items:
for intListindex = 0 to me.combobox1.listcount-1
If Dir(combobox1.list(intListIndex), vbDirectory) = "" Then MkDir combobox1.list(intListIndex)
next intListindex |
Or if you have a variable DirName then just
| If Dir(DirName, vbDirectory) = "" Then MkDir DirName |
____________________________
If you find the answer helpful, please mark this topic as solved.
|
|
30-03-2004 at 10:02 AM |
|
|
Goran Level: Moderator
 Registered: 16-05-2002 Posts: 1681
|
Re: How can i load combobox from ".txt" document?
Hmmm, have you considered importing your CSV file in access database, which will make your coding much easier?
____________________________
If you find the answer helpful, please mark this topic as solved.
|
|
30-03-2004 at 03:35 PM |
|
|
stickleprojects Level: Moderator

 Registered: 09-09-2002 Posts: 891
|
Re: How can i load combobox from ".txt" document?
Apologies... I assumed too much:
The following code will alter a TXT file, however, you will have to split it up into the parts you need.. (also, it assumes at 64k limit on the size of your file!)
NOTE: This is VB6, although I have ignored the *new* rule about not needing the variable name on Next command << i didnt know this till this week, although I've been doing vb6 for about 2 years!!! learn something new every day, eh?!!!! ;)
dim fin as integer
dim strBuffer as string
dim fout as integer
dim lngLine as long
dim iComboIndex as integer
dim arLines() as string
fin=freefile
open "c:\thefileIwantToRead.txt" for input as fin
strBuffer = input(lof(fin),fin)
' Load a listbox with the string from the file
arlines = split(strBuffer,vbcrlf)
for icomboindex = lbound(arlines) to ubound(arlines)
if icomboindex>64000 then
err.raise -1,"too many lines"
exit sub
end if
next iComboIndex
' Load a textbox with the same string
me.txtOutput.MultiLine = true
me.txtOutput.Text = strBuffer
' Save the string from the textbox to a buffer
dim strBufferOutput as string
strBufferOutput = me.txtOutput.text
dim fout as integer
fout=freefile
' NOTE: MyOutputFileName may be the SAME as myinputfilename
open "c:\myoutputfilename.txt" for output as fout
print #fout,strBufferOutput
close #fout
----------------------------------
END
----------------------------------
OK.
This is very simplistic, however, it gets the job done. More specifics will indicate things that we have to get around.
In answer to your Question Ragess.. Yes you can have multiple lines to a text file... eg.
dim fout as integer
dim iLineCount as integer
fout=freefile
open "c:\myfile.txt" for output as fout
for iLineCount=1 to 10
print #fout,"This is a line because it appears after print #fout,"
print #fout,"This is a line but will continue with the previous line because it ends with an ';'";
print #fout," ah-haaaaa! this is on the same line!!! (but the next one isn't!)"
next iLineCount
close fout
How you choose to populate those lines is up to you...
To add lines to a variable of type string.. use the following
[mystringvariablename] = [mystringvariablename] + vbcrlf + [mynewvalue]
E.g.
Create a new VB EXE project
Ensure that a form called Form1 is in the project
In the declarations section of form1:
private mystring as string
Add a CommandButton to Form1
View the properties of this commandbutton
Change it's name to cmdAddStringToFormString
Add a textbox to Form1 called (change the name property) txtNewLine
Add the following:
In the cmdAddStringToFormString_Click code:
mystring = mystring & vbcrlf & me.txtNewLine.text
This will build a string called MyString on your form with the text that the user has entered.
You should be able to piece together the bits of code supplied to do what you want... if not, let us know a bit more detail about the problem and we will supply a complete solution (maybe! ;) )
Hope this helps,
Kieron
____________________________
Build it better, faster, quicker, easier.. then fix it (non-offical MS mission statement)
|
|
30-03-2004 at 10:30 PM |
|
|
|
|
 |
 |