write input values from users to a text file

  • Thread starter Nagendra Singh Dhakar via AccessMonster.com
  • Start date
N

Nagendra Singh Dhakar via AccessMonster.com

i want to write a vba code which will do the following process..

1. prompt user for input like..." enter the node nos of elemnts end points
:"
2. user enter some valuse like.... 2,4
3. write this input to a text file like.... E, 1, 2, 4
where E for element and 1 for no of element

plzzz help me..

nsdhakar
 
J

John Nurick

Do you really want to write just that one brief line of text? If so, you
can do it with something like this, using the WriteToFile() procedure at
the end of this message.

Dim strS As String

strS = InputBox("enter the node nos of elemnts end points:")
WriteToFile strS, "C:\Folder\File.txt", True


i want to write a vba code which will do the following process..

1. prompt user for input like..." enter the node nos of elemnts end points
:"
2. user enter some valuse like.... 2,4
3. write this input to a text file like.... E, 1, 2, 4
where E for element and 1 for no of element

plzzz help me..

nsdhakar


Function WriteToFile(Var As Variant, _
FileSpec As String, _
Optional Overwrite As Long = True) _
As Long
'Writes Var to a textfile as a string.
'Returns 0 if successful, an errorcode if not.

'Overwrite argument controls what happens
'if the target file already exists:
' -1 or True (default): overwrite it.
' 0 or False: append to it
' Any other value: abort.

Dim lngFN As Long

On Error GoTo Err_WriteToFile
lngFN = FreeFile()
'Change Output in next line to Append to
'append to existing file instead of overwriting
Select Case Overwrite
Case True
Open FileSpec For Output As #lngFN
Case False
Open FileSpec For Append As #lngFN
Case Else
If Len(Dir(FileSpec)) > 0 Then
Err.Raise 58 'File already exists
Else
Open FileSpec For Output As #lngFN
End If
End Select
Print #lngFN, CStr(Nz(Var, ""));
Close #lngFN
WriteToFile = 0
Exit Function
Err_WriteToFile:
WriteToFile = Err.Number
End Function
 
N

Nagendra Singh Dhakar via AccessMonster.com

thank you for your help..!

but compiler is showing some error....

"sub or function is not defined"

it is for the following section in WriteToFile function

....CStr(Nz(Var, "")


actually i don't know what type of function is this...how i would define
"Nz".

thanks

nsdhakar
 
N

Nagendra Singh Dhakar via AccessMonster.com

i read whole part and also tried the thing it suggested but error didn,i
solve :(

how can it be removed???

thanks

nsdhakar
 
J

John Nurick

It appears from a separate message of yours in this newsgroup that you
may not be using Microsoft Access. This newsgroup is for questions about
DAO programming in Access, and the Nz() function is specific to Access.

What application are you actually using?

If it's Access, make certain that the references in your project include
"Visual Basic for Applications" and "Microsoft Access X.0 Object
Library", where X is 11 for Access 2003, 10 for Access 2002, and so on.

If it's not Access, you probably won't need to handle the possibility of
a Null value, so just replace
...CStr(Nz(Var, "")
with
...Cstr(Var)
 
N

Nagendra Singh Dhakar via AccessMonster.com

yes, now its working....but the intial problem remains same, its not
printing the node nos in text file. :(...also one more thing if i want to
make it in a loop, means user can enter as many times as he wish, message
board until he returns blank value and write all these values in a
file...how to do it?

thanks

nsdhakar
 
N

Nagendra Singh Dhakar via AccessMonster.com

when i run this code seperately then it works fine but when i include this
in my code where i have included many print command then it doesn't write
the node nos....any suggestions ???

thanks

nsdhakar
 
J

John Nurick

You haven't previously mentioned a problem with node numbers or that you
wanted to write multiple lines to the text file.

It seems that the problem you want to solve is rather different from the
one you initially described. Please can you give a clear explanation of
what it is you want to achieve and whether the data comes from (i.e. is
it all entered by the user, or does some of it have to be retrieved from
a database).
 
N

Nagendra Singh Dhakar via AccessMonster.com

sorry if i have posted wrong problem statement, i forgot to mention about
the multiple lines.what i have wrote in my first posting was just a
example. actually problem statement is..

i want to prompt user for..

"enter force load to keypoints like(n,FY,-280e3),where n is the node no.:"

then user will enter the values like...1, FX,240

this will be written in text file like....

FK,1,FX,240

now again user will be prompt to enter next set of values in same manner.
again i want to write this line in the same format as in the first line

it will be continued untill user enters a null value.

thanks

nsdhakar
 
J

John Nurick

So where is the node number coming from? Does the user enter it, or is
it meant to be generated by your code and presented to the user?

Also, what application are you working in? Is it Access or something
else? If Access, is this part of a database and does this operation have
any connection with other parts of the database?
 
N

Nagendra Singh Dhakar via AccessMonster.com

node no. will be given by user, and user can enter upto L which is the max
no. of nodes (i have generated in my code).
i am working in Access.
thanks

nsdhakar
 
J

John Nurick

I'm finding it rather difficult to understand exactly what it is you
want to do. For example, in a previous message you wrote
then user will enter the values like...1, FX,240

this will be written in text file like....

FK,1,FX,240

which suggested that the "FK" was generated from something other than
user input. In your most recent message you say
node no. will be given by user, and user can enter upto L

Does that mean that the "node number" is actually a letter in the range
A to L? If so, what is FK? Is it necessary to process or validate the
user's input, or merely to write it to the text file exactly as the user
types it?

Please can you try to explain clearly what the situation is, with
examples of (a) what input you expect from the user and (b) what you
want to be written to the text file as a result of that input.
 
N

Nagendra Singh Dhakar via AccessMonster.com

Actually what i want to write in a file is following..

FK,1,FY,-280e3
FK,3,FZ,-210e3
FK,5,FX,-280e3
FK,6,FY,-360e3

in above the variables (for example in first line ) are

1,FY and -280e3

where 1 is corresponds to the node no (can be 1,2,3,...L)
FY corresponds to force in direction X ( can be FX, FY and FZ)
-280e3 is the value of load
and FK is the general term i included in the line to imply Force at key
point.

so what i want is that
user will enter values of these three varriables by himself.
and user can enter values for different nodes as well.

i want to prompt user like...

enter force load to keypoints like(n,FY,-280e3),where n is the node no.:

then he enters the value
now these value will be written in the file as i shown above (FK,1,FY,-
280e3)
put this prompt method in a loop to allow user to enter multiple values, so
that i can print next set of values in my file as i shown in my file.

also one more thing i have written keypoints in my text file initially as
shown below

K, 1,4578.2531,7919.0487
K, 2,11346.8678,11826.9102
K, 3,11346.8678,3072.2707
K, 4,4578.2531,3072.2707
K, 5,11346.8678,7919.0487
K, 6,4578.2531,11826.9102

so we have max no. of keypoints (nodes) as 6. so user can enter force at
keypoints max upto 6 times. this is just a example there can be more no of
nodes in the structure as well.

i think now problem statement is more clear, if still u have any difficulty
in understanding the problem, please post.

please sort out my problem, its urgent..i have less time :(

thanks

nsdhakar
 
J

John Nurick

Something like this, perhaps:


Dim strS As String
Dim lngNodesCount As Long
Dim j as Long
Dim strFileName As String

strFileName = "C:\Folder\File.txt"
lngNodesCount = 6 'or whatever other number

For j = 1 to lngNodesCount
strS = InputBox("Enter force load to keypoints like " _
& vbCrLf & vbCrLf & " n,FY,-280e3" _
& vbCrLf & vbCrLf & "where n is the node no.:")

'append to existing file
WriteToFile "FK," & strS, strFileName, False
Next
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top