Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Visual Basic 6

Options
  • 02-11-2005 12:46am
    #1
    Registered Users Posts: 9,216 ✭✭✭


    Ok, so I know Visual Basic can hardly be considered a true programming language but for my current college project I just need to know one thing as endless googling hasn't given me the answer.

    Currently I have a program that's reading integers from a text file so that the numbers stay constant when the program starts and finishes (we haven't learned how to integreate Databases into our programs yet).

    Anyways at present I have it reading the text file by typing out the full path to the file as it doesn't except just the file name. Is there a way to just get it to read from the textfile of the directory that the form (uncompiled) is currently in?

    Cheers.


«1

Comments

  • Closed Accounts Posts: 2,639 ✭✭✭Laguna


    I doubt it's a spam bot, probably just a funny-guy...

    In answer to your question, I don't think there is a way to refer to the textfile you're talking about with giving an absolute path on the form startup. Someone correct me if I'm wrong, but the only way I could use relative paths in VB when I used to program in it ,was through the use of a common dialog (i.e. .ShowOpen) and literally having to navigate to and select the file you were after (even then, this is at runtime not startup of the program).

    Yeah I'm pretty sure to be honest that absolute paths have to be used when referring to a file at startup as otherwise maybe you could try a reference string that simply says textfile.txt, as it is in the same directory after all


  • Registered Users Posts: 7,677 ✭✭✭Trampas


    Achilles wrote:
    Ok, so I know Visual Basic can hardly be considered a true programming language but for my current college project I just need to know one thing as endless googling hasn't given me the answer.

    Currently I have a program that's reading integers from a text file so that the numbers stay constant when the program starts and finishes (we haven't learned how to integreate Databases into our programs yet).

    Anyways at present I have it reading the text file by typing out the full path to the file as it doesn't except just the file name. Is there a way to just get it to read from the textfile of the directory that the form (uncompiled) is currently in?

    Cheers.

    Typical college student comment about Visual Basic not a programming language.

    All they ever teach you in college is a drop a text box here and button there.

    Its a powerful language when you get to learn (5 years work experience)

    Anyway rant over.

    Post up your code as makes it easier for people to look at what you have done and then can guide you to what you have done wrong to what you should do right. Comment ou code also so you explain what you intend to do here.

    You will get a quicker response.


  • Registered Users Posts: 9,216 ✭✭✭MrVestek


    Ok then, that's extremelly retarded. My lecturer told me that there was a way to do it but he couldn't remember at the time, and with having a week off I haven't had a chance to talk to him yet. He told me to look it up on the net but I can't find anything inr elation to it.

    *edit*

    I don't have the code with me at present as I'm not on my home pc. Anyways I'd prefer not to anyways as there are other guys in my class who were given this exact assignment and I don't want them stealing my code. All I need to know is, how to get it to open, read from, and write to a text file without having to give the full filename path, ie telling it to just use the directory it's in. Say for instances I wanted to read 5 variables from a text file which are numbers seperated by a , like this:

    23, 4, 5, 17, 6

    I can currently do that by telling it:
    Open as #1 "C:\blah\variables.txt"

    But surely there's a way to get it to open variables.txt from within the same folder as the form.


  • Closed Accounts Posts: 2,639 ✭✭✭Laguna


    Yeah post your code up here and my old brain noodle will work better (as I am only recalling VB off of the top of my head here)


    EDIT: Have to agree with Trampas here, what I learnt in college of VB was absolute piss compared to what you learn the language can do yourself/in work. It has the ability to completely control the windows environment and you can make powerful applications in it.


  • Registered Users Posts: 9,216 ✭✭✭MrVestek


    Tell ya what, I'll pm the code to you when I can to keep prying eyes away, ok? By the by the program is only like 70% complete so if you spot any other errors ignore them as I wanna fix them myself.


  • Advertisement
  • Registered Users Posts: 640 ✭✭✭Kernel32




  • Closed Accounts Posts: 2,639 ✭✭✭Laguna


    Kernel32 wrote:


    That's not a helpful comment, I think the charter should say something about posts saying RTFM/unhelpful comments? (if it doesn't already).

    Besides, I had a look at the link, it provides no answer to the mans question, it's not even close. So in future if you're going to shove something in someones face, make sure it's the right information..


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    Laguna wrote:
    That's not a helpful comment, I think the charter should say something about posts saying RTFM/unhelpful comments? (if it doesn't already).

    I believe the charter tells you to RTFM first.

    Hes not really explaining his issue very well, although the link does help AFAIR the app object is set to the location of the EXE on runtime.


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Achilles wrote:
    Ok, so I know Visual Basic can hardly be considered a true programming language...

    Way to go to insult everyone who can help you. Anyway, my vb6 is a little rusty but something along the lines of this (which is what kernel32 was suggesting) should give you an idea:
    dim filePath as string
    
    filePath = app.path & "\" & "doyourownhomeworkinfuture.txt"
    


  • Registered Users Posts: 7,677 ✭✭✭Trampas


    I have done something like this before in work where I read a text file but the information was on a line by line and the printed of the results into another text file

    Names have been changed

    ' Input file ...
    Open "C:\In.txt" For Input As #1

    ' Output file ...
    Open "C:\Out.txt" For Output As #2

    'Here takes in the string from the text file
    Line Input #1, strIn

    'Here outputs the string to text file
    Print #2, strOut

    Infuture post your code as we will be easier for people to respond


  • Advertisement
  • Registered Users Posts: 640 ✭✭✭Kernel32


    Laguna wrote:
    That's not a helpful comment, I think the charter should say something about posts saying RTFM/unhelpful comments? (if it doesn't already).

    Besides, I had a look at the link, it provides no answer to the mans question, it's not even close. So in future if you're going to shove something in someones face, make sure it's the right information..
    From the charter in reference to homework questions:
    Replies may direct the student to the answer, but not post the answer outright. This should help people learn to code, and not how to plagiarise.
    Achilles wrote:
    Is there a way to just get it to read from the textfile of the directory that the form (uncompiled) is currently in?
    From the help file:
    For the App object, Path specifies the path of the project .VBP file when running the application from the development environment or the path of the .exe file when running the application as an executable file.

    As I said before, RTFM and I stand by it. If you slam on the language but then can't be bothered to read the help file then what type of response would be expected?


  • Closed Accounts Posts: 2,639 ✭✭✭Laguna


    Fair enough, I'm always wrong when it comes to my opinion on Boards anyway.


  • Registered Users Posts: 9,216 ✭✭✭MrVestek


    Jesus christ I never should have posted here in the first place. Any time anybody asks for help you always pick apart every little thing that the user asks.

    The program we were asked to create is a Vending Machine Simulation Program. I've explained what I'm trying to do as best as I can. Basically I'm looking to input 5 variables into 5 seperate text files that are retreaved from a text file, do this like so:
    Open "M:\College\CP2\Visual Basic\Vending Machine Project\stock.txt" For Input As #1
    Input #1, CokeStock, DietCokeStock, FantaStock, LiltStock, SpriteStock
    Close #1
    txtCokeQuantity.Text = CokeStock
    txtDietCokeQuantity.Text = DietCokeStock
    txtFantaQuantity.Text = FantaStock
    txtLiltQuantity.Text = LiltStock
    txtSpriteQuantity.Text = SpriteStock
    

    As you can see, for it to work I have to give it the full path of the directory, which in this case is my pen drive. The problem I will have later is if I want to work on it on a college PC or when I submit it on a floppy discs, the paths won't be correct and I can be damned if i'm changing all the file paths (the above aint the only instance in my program of where code is read from a text file) if I'm changing all of the paths manually. If I do something along the lines of this:
    Open "stock.txt" For Input As #1
    Input #1, CokeStock, DietCokeStock, FantaStock, LiltStock, SpriteStock
    Close #1
    txtCokeQuantity.Text = CokeStock
    txtDietCokeQuantity.Text = DietCokeStock
    txtFantaQuantity.Text = FantaStock
    txtLiltQuantity.Text = LiltStock
    txtSpriteQuantity.Text = SpriteStock
    

    Then it doesn't work. So how do I get it to read the variables from a text file?


  • Registered Users Posts: 640 ✭✭✭Kernel32


    Achilles wrote:
    Jesus christ I never should have posted here in the first place. Any time anybody asks for help you always pick apart every little thing that the user asks.
    Did you actually read any of the replies?

    I pointed you to the help file entry that solved the problem.

    Hobbes went one step further and supplied a line of code. I'm not sure what more is needed?


  • Registered Users Posts: 7,677 ✭✭✭Trampas


    Achilles wrote:
    Jesus christ I never should have posted here in the first place. Any time anybody asks for help you always pick apart every little thing that the user asks.

    The program we were asked to create is a Vending Machine Simulation Program. I've explained what I'm trying to do as best as I can. Basically I'm looking to input 5 variables into 5 seperate text files that are retreaved from a text file, do this like so:
    Open "M:\College\CP2\Visual Basic\Vending Machine Project\stock.txt" For Input As #1
    Input #1, CokeStock, DietCokeStock, FantaStock, LiltStock, SpriteStock
    Close #1
    txtCokeQuantity.Text = CokeStock
    txtDietCokeQuantity.Text = DietCokeStock
    txtFantaQuantity.Text = FantaStock
    txtLiltQuantity.Text = LiltStock
    txtSpriteQuantity.Text = SpriteStock
    

    As you can see, for it to work I have to give it the full path of the directory, which in this case is my pen drive. The problem I will have later is if I want to work on it on a college PC or when I submit it on a floppy discs, the paths won't be correct and I can be damned if i'm changing all the file paths (the above aint the only instance in my program of where code is read from a text file) if I'm changing all of the paths manually. If I do something along the lines of this:
    Open "stock.txt" For Input As #1
    Input #1, CokeStock, DietCokeStock, FantaStock, LiltStock, SpriteStock
    Close #1
    txtCokeQuantity.Text = CokeStock
    txtDietCokeQuantity.Text = DietCokeStock
    txtFantaQuantity.Text = FantaStock
    txtLiltQuantity.Text = LiltStock
    txtSpriteQuantity.Text = SpriteStock
    

    Then it doesn't work. So how do I get it to read the variables from a text file?

    1. create a registry file which means you can change it between networks. Get it working locally on C drive before you go deep into folder. Google it

    2. You should have enough now to google it to get further with the Input # as this is part of VB and not a thing we made up.

    3. Go with 1 var first before trying to do 5

    Finally i think college lectures teaching student who know nothing about programming teach them rubbish.


  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    Achilles wrote:
    But surely there's a way to get it to open variables.txt from within the same folder as the form.

    No, there isn't.

    There is a way to open it from the same location/folder as the compiled .exe, but - as has been pointed out - when working in Debug mode this will look for the file in the same location as where the .vbp is stored.

    IN either case, this may be the same folder as "the form" (by which I can only assume you mean the .frm), but that would fall into the category of "programming by coincidence" in my book.

    Why not look at the FileOpen Dialog, and simply get the user to select the file they want to read the values from? Or use a .INI file with the app?

    jc


  • Registered Users Posts: 9,216 ✭✭✭MrVestek


    bonkey wrote:
    No, there isn't.

    There is a way to open it from the same location/folder as the compiled .exe, but - as has been pointed out - when working in Debug mode this will look for the file in the same location as where the .vbp is stored.

    IN either case, this may be the same folder as "the form" (by which I can only assume you mean the .frm), but that would fall into the category of "programming by coincidence" in my book.

    Why not look at the FileOpen Dialog, and simply get the user to select the file they want to read the values from? Or use a .INI file with the app?

    jc
    That's exactly what I need, a way to read the variables from the same directory that the .vbp project is stored, and when the .exe is compiled, to read them from the same directory. We're on the right track now, so how is it done?


  • Closed Accounts Posts: 3,357 ✭✭✭Beano


    Evil Phil has already posted what you need above.


  • Registered Users Posts: 9,216 ✭✭✭MrVestek


    Ahh yes, sorry.

    So I'd just do something like:
    Open as #1 filePath = app.path & "\" & "coins.txt"
    

    Would that work?


  • Closed Accounts Posts: 3,357 ✭✭✭Beano


    sin é


  • Advertisement
  • Registered Users Posts: 9,216 ✭✭✭MrVestek


    Beano wrote:
    sin é
    Cheers guys thanks for the help.


  • Registered Users Posts: 2,781 ✭✭✭amen


    I can be damned if i'm changing all the file paths (the above aint the only instance in my program of where code is read from a text file) if I'm changing all of the paths manually
    you should have a common function for opening files that accepts a path/file name and does the open/close read etc
    that way you only have to change code in one place
    if the same code is in more than 1 place you have done something wrong!


  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    Laguna wrote:
    That's not a helpful comment, I think the charter should say something about posts saying RTFM/unhelpful comments? (if it doesn't already).

    It does indeed; it encourages them for trivial questions such as this.
    Laguna wrote:
    Besides, I had a look at the link, it provides no answer to the mans question, it's not even close.

    Yes it does...
    Trampas wrote:
    Typical college student comment about Visual Basic not a programming language.

    Of course it's a programming language. However, so is INTERCAL.

    Incidentally, I hadn't realised it was taught in colleges. *shudder*


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    Kernel32 wrote:
    Hobbes went one step further and supplied a line of code. I'm not sure what more is needed?

    That was Evil Phil, not me. :)


  • Registered Users Posts: 4,142 ✭✭✭TempestSabre


    Newbie ques always make people grumpy...:D

    Coding requires you to be very precise about that you say and do. It also requires that you read very carefully all the instructions and help files, as porgrammers are often not very verbose (wordy) when explaining or describing things. Since if something is obvious to them, they often assume its obvious to everyone else. This is especially difficult if you are a newbie thats not used to reading carefully. I have a habit of scan/speed reading things, and its a bad habit for coders. Another habit of coders is to only look at, and answer the question thats been asked. Whereas often the question being asked isn't fully formed due to lack of knowledge of the poster. A good coder looks beyond the question, to see if the question being asked is the right question. I think theres should be a few more rules in this forum.

    1) Newbies, RTFM very carefully and slowly, you might have missed something.
    2) If you don't have the patience for newbie ques, don't post in reply.
    3) Don't be pedantic, without explaining why. (A concise answer and RTFM isn't helpful IMO).

    Of course this is kinda in the FAQs and Charter: READ BEFORE POSTING but its kinda longwinded.

    Mind you if you don't want newbie questions in the forum, you should change the forum name to Advanced Programming or similar. Scare newbies away. Just a thought. :cool:


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    I think a newbie programming forum would be a great idea, it would require more *ahem* tolerance on our part - we all had to learn after all :)


  • Registered Users Posts: 640 ✭✭✭Kernel32


    I don't see the problem. My original response was framed within the philosophy of the homework sticky. The original poster indicated this was a college project. The sticky says to not supply code. My reply had no code except a pointer to the help file where the answer could be found. I was then accused of being unhelpful, bizarre to say the least.

    Now at the risk of being seen as an angry old coder, any response beyond the pointer to the help file and maybe a quick one line explanation is doing a disservice to the original poster. At some point the poster well join the workforce and for simple questions like that they will not get any help from senior developers beyond what I provided. If they persisted in ignoring the response as they did here they would soon find themselves looking for a new job. I realize he or she is in college but its all part of learning and it will make them better in the long run.

    I like to see newbie questions. They often start interesting discussions that go beyond the orginal question.


  • Closed Accounts Posts: 2,639 ✭✭✭Laguna


    OK now, let me see if I've got this right..


    This is the Programming forum... yes?

    Why is it taboo/forbidden for people (especially 'newbies') to ask Programming questions?

    Anytime anyone ever asks a question that has a *hint* of a college assignment from it, you're all down their throat deeming yourselves to have carte blanche to pick apart everything and anything they say as you're all uber-L33t HaxxorZ. They should change the name of the forum for it to be more applicable, perhaps to 10010101010101010, to indicate your sheer superiority by communicating via binary.


  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    Any question which can be trivially answered through use of Google or a bit of common sense should not be answered here.

    Bear in mind that ultimately, you may one day end up debugging these peoples' code. With that in mind, it is surely better that they actually learn things, no?


  • Advertisement
  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    Laguna wrote:
    OK now, let me see if I've got this right..


    This is the Programming forum... yes?

    Why is it taboo/forbidden for people (especially 'newbies') to ask Programming questions?

    Its not. Its taboo to be asking questions where the asking and/or subsequent follow-up suggests that you're trying to leech an answer rather than be pointed in the right direction.

    K32 pointed a link to an article discussing the Path property, which mentioned (at the very least) the App object. It was accompanied by an almost-standard programmer-to-programmer comment of RTFM.

    Your response was that it wasn't helpful and didn't give the right answer anyway. The point is that it gave all the pointers one needed to either answer the question directly, or find the answer in short order.

    If you don't see that as helpful, instead preferring that some other programmer hand you the code being asked for, I would humbly suggest you are in the wrong profession.
    Anytime anyone ever asks a question that has a *hint* of a college assignment from it, you're all down their throat deeming yourselves to have carte blanche to pick apart everything and anything they say
    Bear in mind that most of the programmers you're deriding managed to get through college when there was no WWW at hand to supply a handy forum of people to regurgitate knowledge.

    We had to bribe the guys a year in front of us with beer n stuff ;)

    And even then, it involved ridicule.

    One of the greatest skills programmers develop is how to find solutions to problems. While your in college or elsewhere, it may seem that getting other people to tell you what you're missing is a valid way of doing this. In the real world, you'll find that changes.

    Programmers who solve problems by asking other programmers what the solution is....well....ask yourself why you'd hire such a question asker, and not another "question answerer".

    As a result, you'll generally find that programmers who are helping other programmers will not hand over a solution...just a means to find it. If you can find your solution given a pointer, then hopefully your knowledge of where and how to look for pointers will improve and you'll ask less often. You may even understand more of the entire area around which your question was based, rather than just the specific answer to your question.

    K32's link gave the information to solve the problem, plus general information about Path properties. To get the full complete answer, you'd also have to learn a bit (and have the opportunity to learn more) about the App object. So a lot of learning, which would along the way answer the question being asked. Is that not a fair trade?

    If you're response is "that doesn't answer my question, please help", then generally there'd better be a "because..." added at the end to show that you've looked at the pointer and figured out why its wrong.

    Yes, programmers with experience are probably too harsh on beginners. But there's mostly a reason. Give a programmer a hint that you want a spoon-fed answer rather than a pointer....well....just pray you never, ever have to work with or be interviewed for a job by said programmer.

    If the only things beginners learn from such harshness is to be more self-reliant, and to learn how better to find answers from scratch or from pointers....thats worth more than any high grades we can help them obtain by handing them their homework.

    jc


Advertisement