13 Replies - 12924 Views - Last Post: 25 January 2011 - 06:58 AM Rate Topic: -----

#1 Bless841  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 14-January 11

How to call a function in VB.NET

Posted 25 January 2011 - 01:00 AM

Good day,

How can I call function of the form_Load?

I have tried this code but it is not working

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MsgBox (CalcDate(Date, 3))
End Sub

Private Function DecDate(ByVal startDate As Date, ByVal days As Integer) As Date
        Dim dte As Integer
        Dim strDate As Date

        strDate = startDate
        For dte = 1 To days
            strDate = DateAdd("d", -1, strDate)
        Next dte
        DecDate = strDate
        MsgBox("The Date is" & " " & DecDate(startDate, days))
    End Function

This post has been edited by AdamSpeight2008: 25 January 2011 - 03:43 AM
Reason for edit:: Fixing the codetags


Is This A Good Question/Topic? 0
  • +

Replies To: How to call a function in VB.NET

#2 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: How to call a function in VB.NET

Posted 25 January 2011 - 02:09 AM

Hi,

First of all your function has a message box so don't call the function within the message box otherwise there are 2 messageboxes showing. Then you pass "date" to your function have you declared it anywhere?

this is more or less how you do it:

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
   'you will need to know what "Date" is and also instead
   'of Date use MyDate, since Date is a reserved word
   'so let's add one
   Dim MyDate As Date = CDate("01-Jan-2010")
   'you used CalcDate, however your function supplied is DecDate so change it
   DecDate(MyDate, 3)

   'and ultimately you return a date from the function and neve use it
   'maybe it should be a sub???
End Sub

Private Function DecDate(ByVal startDate As Date, ByVal days As Integer) As Date
   Dim dte As Integer
   Dim strDate As Date

   strDate = startDate
   For dte = 1 To days
      strDate = DateAdd("d", -1, strDate)
   Next dte
   DecDate = strDate
   MsgBox("The Date is" & " " & DecDate(startDate, days))
End Function



EDIT:

Pressed a wrong button and made a premature post (HMMM???)

This post has been edited by motcom: 25 January 2011 - 02:12 AM

Was This Post Helpful? 0
  • +
  • -

#3 Bless841  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 14-January 11

Re: How to call a function in VB.NET

Posted 25 January 2011 - 02:32 AM

Hi,

I did everything you've explained but still having some problems.

I am getting an error msg " An unhandled exception of type 'System.StackOverflowException' occurred in WindowsApplication1.exe" on this line

strDate = DateAdd("d", -1, strDate)

This post has been edited by AdamSpeight2008: 25 January 2011 - 03:44 AM

Was This Post Helpful? 0
  • +
  • -

#4 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: How to call a function in VB.NET

Posted 25 January 2011 - 03:20 AM

Does this work?

    DateAdd(DateInterval.Day,-1,strDate)



or even better

    strDate = strDate.AddDays(-1)



also what is the value of strDate before it gets to the "For Loop" ?

what is your code of your Form Load Event Now?
Was This Post Helpful? 0
  • +
  • -

#5 Bless841  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 14-January 11

Re: How to call a function in VB.NET

Posted 25 January 2011 - 03:38 AM

No, it doesn't work have tried both lines.

The value on StrDate is #1/1/2010#. But I want my date to be in this format 20110125

Form load code:
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim MyDate As Date = CDate("01-Jan-2010")
        CalcDate(MyDate, 3)
    End Sub


Function Code :
Private Function CalcDate(ByVal startDate As Date, ByVal days As Integer) As Date
        Dim dte As Integer
        Dim strDate As Date

        strDate = startDate
        For dte = 1 To days
            strDate = strDate.AddDays(-1)
        Next dte
        CalcDate = strDate
        MsgBox("The Date is" & " " & CalcDate(startDate, days))
    End Function

This post has been edited by AdamSpeight2008: 25 January 2011 - 03:45 AM

Was This Post Helpful? 0
  • +
  • -

#6 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: How to call a function in VB.NET

Posted 25 January 2011 - 04:01 AM

Hi,

Well your function call is correct, this is how you would call your function:

CalcDate(MyDate, 3)



but i don't know what you are trying to do with your function

looks like you want to subtract days, well in that case you just use the built in function instead of writing new ones...

so your whole code can be written like this

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim MyDate As Date = CDate("01-Jan-2010")
        'to subtract days
        msgbox(MyDate.AddDays(-3))
        'to add days
        msgbox(MyDate.AddDays(3))
End Sub



no need to make a nother function with for loop and extra variables...
Was This Post Helpful? 0
  • +
  • -

#7 Bless841  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 14-January 11

Re: How to call a function in VB.NET

Posted 25 January 2011 - 04:08 AM

Basically this is what I want to do. I want to upload a batch file from the Server for 3 days before the current date. for E.g Todays date is 20110125, right? My Application should go to the server and search for this file SBSA_PROD_20110122 and upload it to my FTP.

My application can upload the batch file now but now the tricky part I have is how can I make my app upload file for 3 days before the current date.

Here is the code for uploading.

Public Sub DownLoadBatchFile()
        Dim lngWnd As Long
        'Dim strRA As String
        Dim strUserName As String
        Dim strPassword As String
        Dim strServerDir As String
        Dim strLocalDir As String
        Dim strHost As String
        Dim strTxtfile As String
        Dim myStreamWriter As StreamWriter
        Try
            UV_SESSION = UniObjects.OpenSession("ccqs", "root", "combat", "/disk1/cchs", "uvcs")
            dbFTPDetails = UV_SESSION.CreateUniFile("FTP.DETAILS")
            dbFTPDetails.RecordID = "SBSABATCH"
            dbFTPDetails.Read()
            With dbFTPDetails
                strHost = .ReadNamedField("HOST").ToString()
                strUserName = .ReadNamedField("USERNAME").ToString()
                strPassword = .ReadNamedField("PWD").ToString()
                strServerDir = .ReadNamedField("DEST.DIR").ToString()
                strLocalDir = .ReadNamedField("LCD").ToString()
                strFileName = .ReadNamedField("FILENAME").ToString()

            End With
            '' Create ftp Command File
            myStreamWriter = File.CreateText("C:\BATCH.txt")
            With myStreamWriter
                .WriteLine("open" & " " & strHost)
                .WriteLine(strUserName)
                .WriteLine(strPassword)
                .WriteLine(strLocalDir)
                .WriteLine(strServerDir)
                .WriteLine("bin")
                .WriteLine("Ascii")
                .WriteLine("get" & " " & strFileName)
                .WriteLine("bye")
                .Flush()
                myStreamWriter.Close()
            End With

            strTxtfile = "C:\BATCH.txt"
            lngWnd = Shell("ftp -s:" & strTxtfile, vbMinimizedFocus)
            'Sleep(5000)
            'File.Delete("C:\BATCH.txt")

        Catch UniversEx As Exception
            Dim msg As String = UniversEx.ToString()

        Finally
            If UV_SESSION.IsActive Then
                If UV_SESSION.IsActive Then UniObjects.CloseSession(UV_SESSION)
                UV_SESSION = Nothing
                dbFTPDetails = Nothing
            End If
        End Try
        MsgBox("Batch Files downloaded")
    End Sub

This post has been edited by AdamSpeight2008: 25 January 2011 - 04:13 AM

Was This Post Helpful? 0
  • +
  • -

#8 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: How to call a function in VB.NET

Posted 25 January 2011 - 05:35 AM

Ok, well convert todays date to that format and see if that file exists? right?


Private Sub CheckForFile
   'this should convert now to this format "yyyyMMdd"
   'and we will subtract 3 days to get to 
   Dim MyTestDateString As String = DateTime.Now.Year.ToString() & ConvertNumber(DateTime.Now.Month) & ConvertNumber(DateTime.Now.Day.AddDays(-3))

   'Get the MyTestDateString to the format of your File
   MyTestDateString = "SBSA_PROD_" & MyTestDateString 

   'Then see if this file exists on the Server, if not, you know an update file is required surely?
   'This will not work with FTP but you get my point so if you need to do it with FTP then you would
   'still check if the file exists but with FTP routines.
   If My.Computer.FileSystem.FileExists("YourServerPath" & "\" & MyTestDateString) = False Then
      'upload your new file...
   End If

End Sub


'converter as we want a 0 infront of our number of the month and day should it be smaller than 10
Private Function ConvertNumber(ByVal NumberToConvert As Integer) As String
     If NumberToConvert < 10 Then
         Return "0" & CStr(NumberToConvert)
     Else
         Return CStr(NumberToConvert)
     End If
End Function


Was This Post Helpful? 0
  • +
  • -

#9 Bless841  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 14-January 11

Re: How to call a function in VB.NET

Posted 25 January 2011 - 05:55 AM

Thanks,

But I am getting an error on this line, error msg "AddDays is not a member of Integer"

Dim MyTestDateString As String = DateTime.Now.Year.ToString() & ConvertNumber(DateTime.Now.Month) & ConvertNumber(DateTime.Now.Day.AddDays(-3))

Was This Post Helpful? 0
  • +
  • -

#10 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: How to call a function in VB.NET

Posted 25 January 2011 - 05:59 AM

oops, try this

DateTime.Now.AddDays(-3))



or this

DateTime.Now.Day -3


Was This Post Helpful? 0
  • +
  • -

#11 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2264
  • View blog
  • Posts: 9,470
  • Joined: 29-May 08

Re: How to call a function in VB.NET

Posted 25 January 2011 - 06:11 AM

Why not just use the formatting options on the DateTime Object?

Today's date:
Dim day_today=DateTime.Now.ToString("yyyyMMdd")

Yesterday:
Dim day_ystr=DateTime.Now.AddDays(-1).ToString("yyyyMMdd")

Tomorrow:
Dim day_tom=DateTime.Now.AddDays(1).ToString("yyyyMMdd")

Was This Post Helpful? 1
  • +
  • -

#12 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: How to call a function in VB.NET

Posted 25 January 2011 - 06:24 AM

true, so forgot about that one... I normally don't format any dates in my code directly but rather on display format properties on my controls itself...
Was This Post Helpful? 0
  • +
  • -

#13 Bless841  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 14-January 11

Re: How to call a function in VB.NET

Posted 25 January 2011 - 06:46 AM

Thanx is works like a charm.

Your replies are realy helpful.

motcom, you mentioned that I should surely do the update file earlier on if the file does not exist, right?

Well, in my case. The client will upload the files on the server on a daily basis so If there is no file exist meaning that I need not worry about anything so my application need not run if the file cannot be found.

Thanks,
Was This Post Helpful? 0
  • +
  • -

#14 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: How to call a function in VB.NET

Posted 25 January 2011 - 06:58 AM

ok so then make a small list:

1. Like the update file exists (the one from the client) but your program has not sent the new file (check if this file exists).

2. Update file exists (the one from the client) but your program started to update however was interrupted, so you might check if the local file is the same size as the remote file too...

3. Some more possibilities that might exists, like the client file went missing however there is the file from your program.

write them all down and then write some code and at the end of your list test each posibility you could think of again...

happy hunting. ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1