Results 1 to 6 of 6

Thread: [RESOLVED] Save UDP data to file

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2007
    Location
    Sweden
    Posts
    359

    Resolved [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
    Last edited by BULK; Sep 21st, 2007 at 07:38 AM.

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    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
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2007
    Location
    Sweden
    Posts
    359

    Red face 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.

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    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.
    Last edited by Al42; Sep 24th, 2007 at 02:12 PM.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2007
    Location
    Sweden
    Posts
    359

    Talking 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.

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width