Why is this code not functioning properly - Is something missing?
Hope someone can help with this...
this is working but not quite as it should...
this code basically connects to the FTP server and copies the file to the local drive, this works fine...
The problem is if the URL username and password fields are empty you get a pop up message saying that the field cant be empty, however what is happening is after the first error saying no url entered it goes straight to line that starts :
INet = InternetOpen
I always thought that code is executed line by line, whihc means the way it is is it should go through each txt field making sure there is something in them and if not sets the focus back to the field until there is text in the field, when all fields have text in and all is ok, then it should go to the connection process..
PrivateDeclareFunction InternetCloseHandleLib"wininet.dll" (ByVal HINetAsInteger)AsInteger
PrivateDeclareFunction InternetOpenLib"wininet.dll"Alias"InternetOpenA" (ByVal sAgentAsString,ByVal lAccessTypeAsInteger,ByVal sProxyNameAsString,ByVal sProxyBypassAsString,ByVal lFlagsAsInteger)AsIntegerPrivateDeclareFunction InternetConnectLib"wininet.dll"Alias"InternetConnectA" (ByVal hInternetSessionAsInteger,ByVal sServerNameAsString,ByVal nServerPortAsInteger,ByVal sUsernameAsString,ByVal sPasswordAsString,ByVal lServiceAsInteger,ByVal lFlagsAsInteger,ByVal lContextAsInteger)AsIntegerPrivateDeclareFunction FtpGetFileLib"wininet.dll"Alias"FtpGetFileA" (ByVal hFtpSessionAsInteger,ByVal lpszRemoteFileAsString,ByVal lpszNewFileAsString,ByVal fFailIfExistsAsBoolean,ByVal dwFlagsAndAttributesAsInteger,ByVal dwFlagsAsInteger,ByVal dwContextAsInteger)AsBooleanPrivateDeclareFunction FtpPutFileLib"wininet.dll"Alias"FtpPutFileA" (ByVal hFtpSessionAsInteger,ByVal lpszLocalFileAsString,ByVal lpszRemoteFileAsString,ByVal dwFlagsAsInteger,ByVal dwContextAsInteger)AsBooleanPrivateSub btnConnectToFTP_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles btnConnectToFTP.Click
Dim msgAsString =""
Dim INetAsIntegerDim INetConnAsIntegerDim RCAsBooleanIf txtFTP_URL.Text.Trim() =""Thenmsg &=
"No URL Entered"'MessageBox.Show("No URL Entered", "No URL Entered")txtFTP_URL.Focus()
ElseIf txtFTP_UserName.Text.Trim() =""Thenmsg &=
"No User Name Entered"' MessageBox.Show("No User Name Entered", "No User Name Entered")txtFTP_UserName.Focus()
ElseIf txtFTP_Password.Text.Trim() =""Thenmsg &=
"No Password Entered"' MessageBox.Show("No Password Entered", "No Password Entered")txtFTP_Password.Focus()
EndIfIf msg.Length <> 0ThenMessageBox.Show(msg,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error)EndIfINet = InternetOpen(
"MyFTP Control", 1, vbNullString, vbNullString, 0)INetConn = InternetConnect(INet, txtFTP_URL.Text, 0, txtFTP_UserName.Text, txtFTP_Password.Text, 1, 0, 0)
RC = FtpGetFile(INetConn,
"/test/test1.txt","c:\FTP\test\test1.txt",True, 1, 0, 0) If RCThen MessageBox.Show("Transfer succesfull!")Else MessageBox.Show("Transfer Failed","There was an error in connection")InternetCloseHandle(INetConn)
InternetCloseHandle(INet)
EndSub
