Solved

Using FTP with VB

Posted on 2002-03-13
7
429 Views
Last Modified: 2007-12-19
I want to ftp to a Unix machine in VB and was looking at the Internet Transfer Control. The problem with the control is that it appears to be intended for anonymous sites.
I need to pass a login and password. Is there an API that I can use to invoke the standard ftp tool in Windows?

1. Is this the best way to accomplish what I'm trying to do?
2. Does anyone have any sample code they could share?

Thanks..
0
Comment
Question by:GeekMan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 3

Expert Comment

by:DennisL
ID: 6861523
www.vbcode.com

do a search for FTP and you'll get a bunch of sample code
0
 
LVL 6

Expert Comment

by:ebosscher
ID: 6861740
GeekMan,

the Inet control will allow to log in using a username and password.

the code looks like this:

With itcFTP
   .URL = strIP
   .Protocol = icFTP
   .RemotePort = 21
   .UserName = strUserId
   .Password = strPassword
End With


after you do that you can execute commands

itcFTP.Execute , "PUT " & strFileName & " " & strRemoteFileName
zsWait


the zsWait function looks like this:

Private Sub zsWait()
    ' keep going until we can stop
    While itcFTP.StillExecuting = True
        DoEvents
    Wend
End Sub


and in the same form you can use the function itcFTP_StateChanged(ByVal State As Integer) as a callback function allowing you to do things while the control is executing (like check the state variable to check what it's doing.

If you still don't like the Inet control look into the wininet.dll file, it contains function to allow you to do FTP (I believe it is a wrapper for the FTP functions of winsock)

Hope this helps,

Evan
0
 
LVL 6

Expert Comment

by:ebosscher
ID: 6861743
Oh, in my prevous comment.. the itcFTP_StateChanged function (Sub) is defined for you as the callback function.. all you need to do is code the function
Cheers
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:MikeRenz
ID: 6861927
you can connect to an ftp site like this:

ftp://username:password@ftp.myftp.com/

that way it won't be anonymous.
0
 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 100 total points
ID: 6862418
from msdn:
' there is a little trick to get to work...
HOWTO: Internet Transfer Control Using Username and Password
Last reviewed: October 16, 1997
Article ID: Q173264  
The information in this article applies to:
Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, version 5.0


SUMMARY
When setting the Internet Transfer Control URL, Username, and Password properties programmatically, the order in which you do it is very important. If you don't set the URL property before the Username and Password properties, you will get the following error:


   Run-time error '35754'
   Unable to connect to remote host


IMPORTANT: In the control shipped with Visual Basic 5.0 (not SP2) the Username is sent as Anonymous if the URL property is not set first (regardless of the UserName and Password properties). So when connecting to an FTP server, you would be accessing the URL with the Anonymous credentials, not with the credentials you intended (according Username and Password Inet properties).
NOTE: The Internet Transfer Control included with Visual Studio 97 Service Pack 2 is unable to make an authenticated connection using the Username and Password properties. For more information, see the following article in the Microsoft Knowledge Base:


   ARTICLE-ID: Q173265
   TITLE     : BUG: Internet Transfer Control (SP2) Username and Password


Workaround
This is sample code for setting the URL property before the Username and Password properties so you won't get the error described above:


   Inet1.URL = "ftp://YourFTPServer"
   Inet1.UserName = "Username"
   Inet1.Password = "Password"


Notice the URL property is set before the Username and Password properties.
This information is also documented in the Visual Basic 5.0 Readme file.



MORE INFORMATION


Steps to Reproduce Behavior

Start a new Standard EXE project. Form1 is added by default.

From the Project menu, click Components and select the Internet Transfer Control 5.0 into the project if it is not already loaded.

Place an Internet Transfer Control 5.0 (Inet1) on Form1 and add the following code:


      Private Sub Inet1_StateChanged(ByVal State As Integer)
       Dim vtData As Variant ' Data variable.



       Select Case State
       ' ... Other cases not shown.
       Case icError ' 11
           ' In case of error, return ResponseCode and

      ' ResponseInfo.
           vtData = Inet1.ResponseCode & ":" & _
           Inet1.ResponseInfo

       Case icResponseCompleted  ' 12
           Dim strData As String: strData = ""
           Dim bDone As Boolean: bDone = False

           ' Get first chunk.
           vtData = Inet1.GetChunk(1024, icString)
           DoEvents

           Do While Not bDone

               strData = strData & vtData
               ' Get next chunk.
               vtData = Inet1.GetChunk(1024, icString)
               DoEvents

               If Len(vtData) = 0 Then
                   bDone = True
               End If
           Loop

          MsgBox strData
       End Select
      End Sub



Place a CommandButton (Command1) on Form1 and add the following code:


      Private Sub Command1_Click()
          Inet1.UserName = "Username"
          Inet1.Password = "Password"
          Inet1.URL = "ftp://YourFTPServer"

          Inet1.Execute , "DIR"
     End Sub



From the Run menu, click Start (ALT, R, S) or press the F5 key to run the program. Click the Command1 button, and you will see error described above.  
0
 

Author Comment

by:GeekMan
ID: 6862488
All good answers with useful tips I can really use. Will gladly post additional points for others that responded upon request.
Thanks.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6864431
Thanks for "A" grade!
I didn't try it but i like MikeRenz's comment.
cheers
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question