borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (Text to Listbox error - whats wrong with my code?)Next Topic (help with Access Email procedure..) New Topic New Poll Post Reply
AndreaVB Forum : VBA (Access, Excel, Word, ...) : How can i load combobox from ".txt" document?
Poster Message
ragess
Level: Guest


icon How can i load combobox from ".txt" document?

Please help me i'm just a beginer.
i want to write my items in ".txt" file & load all items in combobox. how can i do it. how can i do it with newly added item, would it just load it self?
     Next i want to create folders for each item that is loaded in the combobox. How do i do it?
     One more question can i make pre typed word document to open? i mean can i open a questionaire made in word & save it after filling some informations without changing the previous questionaire?
PLEASE HELP ME.
  

21-03-2004 at 12:41 PM
| Quote Reply
stickleprojects
Level: Moderator


Registered: 09-09-2002
Posts: 891
icon 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
View Profile Send Email to User Show All Posts | Quote Reply
ragess
Level: Guest

icon Re: How can i load combobox from ".txt" document?

Thank you Kieron!
i'm thankful for your help. but there is one more thing where i deed your help.
well i want to enter Items through a textbox & i want to add this Item to the ".txt" file and load this Item to the combobox at the same time.I tried with "refresh" property but it did'nt work. What i actually want is add Items ( and delete also) to the ".txt" file through textbox and command button, load the Items in the combobox, make directory with the Item name.
     I could'nt creat multy line text in ".txt" file and edit it in the run time. So please help me.
Than you.
  

22-03-2004 at 06:31 AM
| Quote Reply
neutrall
Level: Master


Registered: 28-03-2004
Posts: 122
icon Re: How can i load combobox from ".txt" document?

Here's How to Open you text File  and load it into a combo box :



Dim InFile As Integer
InFile = FreeFile
Open "MYFILE.TXT" For Input As InFile
While Not EOF(InFile)
  Line Input #InFile, NextTip
   Combobox1.additem NextTip
Wend
Close InFile



When Closing Your Form do the following :


InFile = FreeFile

Open "MYFILE.TXT" For output As InFile
for intListindex = 0 to me.combobox1.listcount-1
    Line print#Infile, combobox1.list(intListIndex)
next intListindex


This should put all the info of you'r combo box back into your Text file.

____________________________
A Stick give a wise man something to think about... and a fool, something to put in is mouth.

28-03-2004 at 02:54 AM
View Profile Send Email to User Show All Posts Visit Homepage ICQ | Quote Reply
ragess
Level: Guest

icon Re: How can i load combobox from ".txt" document?

quote:
stickleprojects wrote:
me.combobox1.additem aline

.................................................
dim intListIndex as integer

for intListindex = 0 to me.combobox1.listcount-1
    mkdir me.combobox1.list(intListIndex)
next intListindex



Thank you for the help

I tried with your code & it worked marvelously .
         But i got another problem, every time i loaded combobox with command button the list that was in the combobox used to be added again.
               So i did combobox1.clear 1st & continued with the code. Trouble was not there BUT is it good way?
   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?
           So again i tried to enter 'names' through a text box, make a directory 1st & enter that "name" to ".txt" file then to load all the list from ".txt" file to combobox. I couldn't enter more than one 'name' in the ".txt" file & couldn't load on combobox. .     Please help.
How can i make directories in a specified path. I tried but it gave error.
Help me again. Thank you.
29-03-2004 at 11:47 AM
| Quote Reply
Goran
Level: Moderator

Registered: 16-05-2002
Posts: 1681
icon 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
View Profile Send Email to User Show All Posts | Quote Reply
ragess
Level: Guest

icon Re: How can i load combobox from ".txt" document?

quote:
Goran wrote:


Hi Goran.
well can you tell me how can I add multiple line of text in ".txt" file. Can i select them and delete them through a textbox & a command button.

quote:
If Dir("Mydir", vbDirectory) = "" Then MkDir "MyDir"



           Can you tell me how can i know the existance of a directory if I don't know the name of the directory, I may not know what name i might enter to make a directory.
Thank you   
30-03-2004 at 08:17 AM
| Quote Reply
Goran
Level: Moderator

Registered: 16-05-2002
Posts: 1681
icon 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
View Profile Send Email to User Show All Posts | Quote Reply
ragess
Level: Guest

icon Re: How can i load combobox from ".txt" document?

Thank you Goran
Can you tell me whether i can have multiple line written to a .txt file. I mean to say whether I can enter List of names one at a time through a Text box & command button?
         Can I add or remove a Name in ".txt" file through a Text box & command buttom?
Help me.

30-03-2004 at 01:55 PM
| Quote Reply
Goran
Level: Moderator

Registered: 16-05-2002
Posts: 1681
icon 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
View Profile Send Email to User Show All Posts | Quote Reply
stickleprojects
Level: Moderator


Registered: 09-09-2002
Posts: 891
icon 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
View Profile Send Email to User Show All Posts | Quote Reply
ragess
Level: Guest

icon Re: How can i load combobox from ".txt" document?

Hey Pals!!
Can I do this, creat a sub-dir with a name entered through a textbox & command button inside a Dir & load all sub-dir names into combo box & then by selecting a specific sub-dir name from Combo box list all the file names inside that particular sub-dir into a list box.
              But can you tell me how do i make  main Dir 1st & then sub-dir in the specifid Path name(inside this main Dir). How can i avoid duplicating sub-dir names, how can i remove any sub-dir. How can i avoid errer message that occurs if all the sub-dir are deleted.
              Can i add or remove file/s that are loaded in the listbox & how can i open a selected file. Can i open multiple files, without closing the previous file.?

Can you help me?
Thank you.

31-03-2004 at 11:32 AM
| Quote Reply
AndreaVB Forum : VBA (Access, Excel, Word, ...) : How can i load combobox from ".txt" document?
Previous Topic (Text to Listbox error - whats wrong with my code?)Next Topic (help with Access Email procedure..) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder