Check for the existance of a file!! |
Public Function FileExist(parmPath As String) As Integer FileExist = Not (Dir(parmPath) = "") End Function' FileExist |
Start a Dialup Internet connection from a VB application in Windows 95 |
Private Sub StartConnection() Dim X X = Shell("rundll32.exe rnaui.dll,RnaDial " & Your_Connection_Name, 1) DoEvents SendKeys "{enter}", True DoEvents End Sub |
Detect the first non writable device like cd-roms, network drives |
Determine Drive Types and Next Available Drive Letter Declare Function GetDriveType _ Lib "kernel32" Alias "GetDriveTypeA" ( _ ByVal nDrive As String) _ As Long Function FreeDrive() As String Dim DriveNum As String 'To cycle through drive letters in order Dim DriveType As Long 'To hold the type of drive it is DriveNum = 64 'Prime the variable to be used in the loop Do DriveNum = DriveNum + 1 ' start at drive zero. DriveType = GetDriveType(Chr$(DriveNum) & ":\") ' If we are past C: and the drive type is indeterminate, exit the Loop If DriveType = 1 And DriveNum > 67 Then Exit Do Select Case DriveType Case 0: MsgBox Chr$(DriveNum) + ": is An Unknown type" Case 1: MsgBox Chr$(DriveNum) + ": Does Not Exist" Case 2: MsgBox Chr$(DriveNum) + ": is a Removable Drive" Case 3: MsgBox Chr$(DriveNum) + ": is a Fixed Drive" Case 4: MsgBox Chr$(DriveNum) + ": is a Remote Drive" Case 5: MsgBox Chr$(DriveNum) + ": is a CD-ROM Drive" Case 6: MsgBox Chr$(DriveNum) + ": is a RAM Drive" End Select Loop FreeDrive = Chr$(DriveNum) + ":" 'Return the next available drive letter End Function Private Sub Form_Click() MsgBox "Next Available Drive letter is " & FreeDrive() End Sub |
Shut Down Windows from VB |
This code will be especially useful in setup programs when you want to restart the system, so that it refreshes everything, and the program becomes properly runnable. Anyway, here's the code to shut down Windows 95. 1. Place this code in the Declarations section of the Form: Const EWX_LogOff As Long = 0 Declare Function ExitWindows Lib "User32" Alias "ExitWindowsEx" (ByVal dwOptions As Long, ByVal dwReserved As Long) As Long 2. Add this code to where you want to shut down from (eg. Command1_Click): ExitWindows EWX_LogOff, &HFFFFFFFF This code will only work from an EXE file. That means that you will have to compile this fila as an EXE, and if you want to, save this project. When the project is compiled, close all instances of VB you have open, and probably all other programs you would have open. Run the EXE, and click on Command1 (or whatever you made the trigger as). The Windows session should shut down. |
Copying Files |
This routine will copy a file while providing a means to support a percent gauge. Ex. your display routine is called "PercentDone" and accepts the values 0-100. Error support is provided. By L. Serflaten (serflaten@usinternet.com) Function CopyFile (src As String, dst As String) As Single 'L. Serflaten 1996 Static Buf$ Dim BTest!, FSize! Dim Chunk%, F1%, F2% Const BUFSIZE = 1024 'This routine will copy a file while providing a means 'to support a percent gauge. Ex. your display routine 'is called "PercentDone" and accepts the values 0-100. 'Error support is provided. ' 'A larger BUFSIZE is best, but do not attempt to exceed '64 K (60000 would be fine) ' 'The size of the copied file is returned on success '0 is returned on failure If Dir(src) = "" Then MsgBox "File not found": Exit Function If Len(Dir(dst)) Then If MsgBox(UCase(dst) & Chr(13) & Chr(10) & "File exists. Overwrite?", 4) <> 6 Then Exit Function Kill dst End If On Error GoTo FileCopyError F1 = FreeFile Open src For Binary As F1 F2 = FreeFile Open dst For Binary As F2 FSize = LOF(F1) BTest = FSize - LOF(F2) Do If BTest < BUFSIZE Then Chunk = BTest Else Chunk = BUFSIZE End If Buf = String(Chunk, " ") Get F1, , Buf Put F2, , Buf BTest = FSize - LOF(F2) ' __Call percent display here__ 'PercentDone ( 100 - Int(100 * BTest/FSize) ) Loop Until BTest = 0 Close F1 Close F2 CopyFile = FSize Exit Function FileCopyError: MsgBox "Copy Error!" Close F1 Close F2 Exit Function End Function You would use it as (I think - L. Serflaten is this it?) ProgressBar1.Value = CopyFile (Which_File*, To_Where**) * = Which File to copy ** = To where to copy the File. Thanks L. Serflaten. |
Clearing all Text Boxes |
Sometimes you need to clear all of the textboxes on a form. For example, this is done when you click the reset button on a HTML Form. So, here is the code for it. Sub ClearAllTextBoxes(frmTarget As Form) For i = 0 To (frmTarget.Controls.Count - 1) Set ctrlTarget = frmTarget.Controls(i) 'If it's a textbox, clear it If TypeOf ctrlTarget Is TextBox Then ctrlTarget.Text = "" End If Next i End Sub This clears all the text boxes. You call it by calling ClearAllTextBoxes FormName It's that simple. |
Unloading All Forms |
There has been a lot of stories about how Visual Basic doesn't unload the forms when you exit the program. This is a 'resource killer'. So here is the code I received from somewhere. This unloads all of the forms in your program. Public Sub UnloadAllForms(sFormName As String) Dim Form As Form For Each Form In Forms If Form.Name <> sFormName Then Unload Form Set Form = Nothing End If Next Form End Sub This is a sub, that you would probably use from the Form_Unload of your Main form. So here is the code for that: Call UnloadAllForms Me.Name Also, here is the code if you're calling it from other Subs: Call UnloadAllForms "" |
Quicker Form Movement |
Did you know that the Move Method is 45% faster than setting the Left, then the Top of the form?! It is also much better looking when you just move the Form to one positieon than left, and the Top, especially on slower systems, where is will be quite visible. So, basically here is the code for the move Method: Sub ... Form.Move x, y End Sub... It is as simple as that. |
Getting System Directory |
When you're creating an application (that uses an INI file for example), most of the time you'll need to find out the user's \System Directory. Most of the time it would be 'c:\windows\system\", but you really cannot rely on that. Each user may install the OS differently, and here is the code you should use. Insert this code into the declarations section of the Module: Declare Function GetSystemDirectory Lib "Kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long Function VBSysDir() As String Dim Gwdvar As String, Gwdvar_Length As Integer Gwdvar = Space(255) Gwdvar_Length = GetSystemDirectory(Gwdvar, 255) VBSysDir = Left(Gwdvar, Gwdvar_Length) End Function Now, you would basically use this Function as (for example): MsgBox VBSysDir & " is the System Directory", vbInformation, "System Directory" |
Enable Drag and Drop of Files |
Having Drag and Drop Capabilities in your program is a major advantage. Visual Basic Island has a sample project which demonstrates how to do this. Just download it, and see how it works!Download a Sample Project which Demonstrates how to do this from VB. dragdrop.zip 4.04 KB ---------------------- dragdrop.vbp dragdrop.bas form1.frm form1.frx readme.txt |
Detecting and activating a previous instance of your application This routine will prevent two copies of your program from running at the same time. It consists of a Function that determines if another instance is already running and activates it if it is. The Sub (Form_Load()) calls this function and closes the program if there is another instance of the program running. Function AnotherInstance () As Integer Dim AppTitle$ If App.PrevInstance Then ' Hold the title of the application (title bar caption) AppTitle$ = App.Title ' Change our application title App.Title = "No longer want this app running..." ' Activate the previous instance AppActivate AppTitle$ ' Let calling procedure know another instance was detected AnotherInstance = True Else ' Let calling procedure know another instance was NOT detected AnotherInstance = False End If End Function Sub Form_Load () ' Don't want two copies of the program running at the same time If AnotherInstance() Then End ' Note: that this routine will not work if the application's title changes ' (showing file names in the title bar for example). End Sub |