[RESOLVED] Save UDP data to file
Hi, all
I have an application that listen to a port, and receives UDP data. The data is sent to a textbox.
I want to add a checkbox, and when the checkbox is =1 then I want the UDP data to be saved to a file on desktop named with date and time.
I made this code, and it does the job. But it doesn't make a new file when I check and uncheck the checkbox. I can only make one log, then I need to restart my program, to make a new log.
Private Sub sckUDP_DataArrival(ByVal bytesTotal As Long)
On Error Resume Next
If Err Then MsgBox Error$, 48
Dim filelocation As String
Dim strData As String
sckUDP.GetData strData 'Retrieve the data
txtReceive.Text = strData 'Display it
If chkLog = 1 Then
filelocation = "UDP_Log" & "-" & Format(Now, "yyyymmdd hhmmss") & ".txt"
Open filelocation For Append As #1
Print #1, txtReceive.Text
If chkLog = 0 Then
Close #1
End If
End If
End Sub
Re: Save UDP data to file
Use this to create the file name instead of your code
Code:
Option Explicit
Dim strFileName As String
Private Sub chkLog_Click()
'Create a new file name each time the box is checked
If chkLog.Value = vbChecked Then
strFileName = "UDP_Log" & "-" & Format(Now, "yyyymmdd hhmmss") & ".txt"
End If
End Sub
Then
Code:
Private Sub sckUDP_DataArrival(ByVal bytesTotal As Long)
Dim filelocation As String
Dim strData As String
On Error Resume Next
sckUDP.GetData strData 'Retrieve the data
txtReceive.Text = strData 'Display it
If chkLog.Value = vbChecked Then
Open strFileName For Append As #1
Print #1, txtReceive.Text
Close #1 'Close it every time
End If
End Sub
Re: Save UDP data to file
Hi, Thank's for helping me.
I'll try the code when I come back to work on monday. Where my devices are.
I'm not a skild programmer in VB, and I have sometimes failed using Option Explicit. If I do can I Dim strFileName As String in both chkLog_Click and sckUDP_DataArrival with the same result? Again tnx 4 helping me.
Re: Save UDP data to file
Whether you use Option Explicit or not, you can't. The variable has to have the same data in it in both subs so it has to be declared outside the subs. A variable exists only during its scope (which is the part of the code in which it's declared) so a variable that has to hold the same data in more than 1 sub has to be declared outside them. (If it has to hold a value over more than 1 form it has to be declared outside any forms, which is the main use of Modules - global variabls and globally-available subs and/or functions.)
Option Explicit merely requires that all variables have to be declared. That means that, for example, if you declare sMyString but use sMySting, the compiler will point out your spelling error.
Re: Save UDP data to file
Hi, the code works, great. Thank's.
Now I'm having an other problem with a FTP client, but I'll post it as a new issue.
Re: Save UDP data to file
If you consider this resolved, you could help us out by pulling down the Thread Tools menu and clicking the Mark Thread Resolved menu item. That will let everyone know that you have your answer.
Thank you. :)