Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to create log files 1

Status
Not open for further replies.

dekcool

Programmer
Oct 2, 2002
231
PH
hi! good day!

how to create log files in text file?
sample daily log file on how to create and edit log of the day.



any input(s) will appreciate. thanks in advance.


____________________________________________________
Invest your time in learning, Not just practicing.

DEK
 
The code below will create a log file directly on C:, change to your desired directory or app.path & "\Test.log"


Open "C:\Test.log" for output as #1
Print #1, "What I want in the log"
Close #1


Matt
 

Hmmm... more to the point the Output parameter will overwrite the file and you will lose any previous data in the file. Use Append instead if as you say you want to edit the log (or you can still use it to create the log).

Good Luck

 
Absolutely correct. If you have msdn installed press F1 on the word Open and you will see what you can do with that statement.

Matt
 
thanks guys!

but iam novice. pls be more specific be give me sample code.

thanks in advance.

____________________________________________________
Invest your time in learning, Not just practicing.

DEK
 
that creates the log, of whatever is in the " ", how do I create a log of something that changes. eg log of users logging in.

heres some code i've been playing around with:
Private Sub cmdOK_Click()


If txtUserName = "til" Then
LoginSucceeded = True
Else
MsgBox "Invalid username, try again!", , "Login"
txtPassword.SetFocus
SendKeys "{Home}+{End}"



End If
If txtPassword = "hello" Then
LoginSucceeded = True
Form5.Visible = True
frmLogin.Visible = False

Else
MsgBox "Invalid password, try again!", , "Login"
txtPassword.SetFocus
SendKeys "{Home}+{End}"

End If

Open "C:\login.log" For Append As #1
Print #1, "login succeeded 'txtusername'"
Close #1


End Sub

The end is where I have added the log bit, but that phisically prints what is within the quotes. How do I get it to actually check which user is logging in and to get it to log that. I know my code only supports the login or user: til password: hello, also, how do I create a database of all the usernames and passwords, instead of encorporating them into the code.

many thanks for any help
 
The txtusername must be outside of the quotation marks if you want to print the value of the variable.
Here's an example:

Open "C:\login.log" For Append As #1
Print #1, "login succeeded " & txtusername
Close #1
 
thanks again guys!

but this is how i will create text file or log file
ea: the date is 5/11/2004

and text file format will be "log51104.txt"

then the system will check if the file is exist
if the file exist then add log message
else it will create new text file and add log message

my question is what function i will use to create text file
and how can i add text message on log file.

thanks again!


____________________________________________________
Invest your time in learning, Not just practicing.

DEK
 
Just use concatenation operator to generate file name, eg:
[tt]
strFName="log" & format(date,"ddmmyy") & ".txt"
[/tt]
Then use Dir function to see if it exists:
[tt]
If Len(Dir(strFName)) > 0 Then
' it's there, open for append
Else
' it's not there open for output
End If
[/tt]

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Hi,

I wish to create a log file in my project. This file should log each even that happens during my code execution. For example,
"Connect to Database Successful"
Server = "xyz"
Database = "abc" etc.

These events are scattered all over the project in different modules and functions. Now, I tried to define a string
Dim strlogevent as string.

Each time I wish to log an even, I am assigning a valid message to strlogevent and then open the text file in append mode, write this to it and close this. But this is making my code look ugly. I see
Open (logfile) for append as #1
print #1,strlogevent
close #1
all over the project.

Can I have something like a function to which I can pass this message which will log this message ? I am not sure how to define this.

Can someone help me ? Is there any better way to do this ? Please help.
 
Make your Sub in a BAS module:

Public Sub LogThis(strToLog as String)
Open (logfile) for append as #1
print #1,strToLog
close #1
End Sub

then in rest of your project:

LogThis "Connect to Database"

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first
'If we're supposed to work in Hex, why have we only got A fingers?'
Essex Steam UK for steam enthusiasts
 
Hi John..

Thanks a lot for the solution.

If you don't mind can you explain the difference between normal modules and a BAS module ? I have never used BAS modules before. So am curious to learn and know. To create a BAS module, can I create a normal module and name is with a .BAS ?

Thanks in advance.

 
Sorry. Got confused while asking the above. What I meant to ask was the difference between a module and a class module.

Thanks
 
Have you looked at the faq in my sig yet? It contains some guidelines on forum use.

For your basic research you should try Google or MSDN - for this question (for instance):
which points to the basic differences.

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first
'If we're supposed to work in Hex, why have we only got A fingers?'
Essex Steam UK for steam enthusiasts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top