Sunday, 22 September 2013

How to get list of all files and folders starting from a specific path - Visual Basic 6.0

option explicit
private fs as Object     ' declared for filesystem object
private flds as Object    ' declared for main folder object
private fld as Object    ' declared for folder object
private fl as Object      ' declared for file object

private sub ListFolderAndFiles(pathname as String)
   create file system object
   set fs=CreateObject("Scripting.FileSystemObject")

   'exit if fails to create file system object
   if fs is nothing then exit sub

  ' exit is folder does not exists in the pathname
  if not fs.isFolderExists(pathname) then
      msgbox "Ivalid path or folder name"
      exit sub
  end if
 
   ' Set the folder object from the specified path
   set flds=fs.GetFolder(pathName)
  
   ' exit if failed to create folder object
   if not flds is nothing then exit sub

  ' enumerate through all folders in the parent folder
   for each fld in flds.Folder
     ' enumerate through all files in the folder
       for each fl in fld.Files
           ' add filename along with it's containing folder name  in the listbox
           call list1.AddItem(fld.Name & "-" & fl.Name)
       next fl
   next fld
 
   'release object from memory
   set flds=nothing
   set fl=nothing
   set fs=nothing
End Sub

Private Sub Form_Load()
     ' Calling procedure
     call ListFolderAndFiles("c:\MyLibrary")End Sub

How to enable, disable controls in your form in Visual Basic 6.0

' procedure will take parameters as following
  form object, enable or disable flag, types of controls to be enabled or disabled

option explicit
Private sub EnableDisableControls(frm as Object,flag as Boolean,ParamArray arg() as Variant)
   dim ctl
'   loop starts to fetch all controls in the form object
   for each ctl in frm.Controls
        dim i as integer
  'inner loop starts to navigate through arg from lower bound to upperbound for all the typename parameters passed by in the procedure

        for i=lbound(arg) to ubound(arg)
'typename return control type name, if it matches with the typename send in the argument then ctl.enabled state is set to flag value.
            if vba.lcase(vba.typename(ctl)) = vba.lcase(arg(i)) then
                 ctl.Enabled = flag
                 doevents
           end if
        next i
   next ctl
End Sub

Writing a simple Text Validation function in Visual Basic 6.0 IDE

option explicit
' Enum created for validation type
private enum enumValidation
   numericOnly
   alphabetOnly
end enum

private sub ValidateText(ctl as object,byref keyascii as integer,e as enumValidation)
   ' parameter ctl is a by reference to text object, keyascii is the ascii key code value pressed by  user send as byref so that we can modify the value, e is the validation type

          ' if the validation is not met type then keyascii is set to 0 which will prevent the value of the key pressed by user to be printered in the textbox

   select case e
      case numericOnly
        if not isNumeric(keyascii) then               keyascii=0
        end if
       case textOnly
        if  not (keyascii>=65 and keyascii<=90) or (keyascii>=97 and keyascii<=122)  then
               keyascii=0
        end if
        end select        
End Sub

private sub Text1_Keypress(KeyAscii as Integer)
    call ValidateText(text1,Keyascii,alphabetOnly)
End Sub

How to use extendable parameter list in functions using paramarray in Visual Basic 6.0

' This program will call a function which can take as many parameters as possible. Parameters not predefined.

option explicit

private sub PrintName(ParamArray arg() As Variant) ' argument is of type variant (can store any type of value) paramarray, function can be called with arguments which is flexible.

   dim i as integer
   for i=lbound(arg) to ubound(arg)
     ' lbound is the lower bound of the array and ubound is the upper bound of the array
       debug.print arg(i)
   next i
End Sub

How to get the list of all the controls in a form in Visual Basic 6.0

'This program will retrieve all the controls in the form and the name of the controls and their type in a list box.

option explicit
private ctl as object

private sub Form_Load()
   for each ctl in Me.Controls
      ' this listbox will contain name and types of all the controls including itself.
       list1.Additem ctl.Name & "," & vba.typename(ctl)
       doevents
   next ctl   
End Sub

How to get computer name using windows API in Visual Basic 6.0

' Platform : any windows OS
' Developing platform visual basic 6.0 (Visual Studio 6.0 IDE)
 '------------------------------------------------------------------------------------------------------------------
option explicit
' GetComputerName is a windows API to retrieve computer name
Private ComputerName as String
Private strBuffer As String
Private lngBufSize As Long
Private lngStatus As Long
private computername as string

Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Private sub Form_Load()

' before passing strBuffer as by reference to "GetComputerName" function, size need to be allocated for strBuffer which is assigned lngBufSize

    lngBufSize = 255  

    strBuffer = String$(lngBufSize, " ") ' Memory allocated for strBuffer

 ' lngStatus returns status for success or failure. return any non zero value for success. computer name is set in the strBuffer string and length of the string is assigned in lngBuffsize, i.e. if  computer name is "subhroneel" then strbuffer is set with "subhroneel" and lngBuffSize is set with 10

    lngStatus = GetComputerName(strBuffer, lngBufSize)

    If lngStatus <> 0 Then
        ' extra allocated space is truncated
        computername = Left(strBuffer, lngBufSize)
    End If

End Sub

How to get list of field names of a table using activex dataobject in VB6

Database used : Oracle 11g
Client used : Oracle Net8 client
Application developed in Visual Basic 6.0

We are going to print the output in a multiline textbox
'---------------------------------------------------------------------------------------------------------------------
' Declaration
option explicit
private con as ADODB.Connection
private rs as ADODB.Recordset

private sub Form_Load()
  set con = new ADODB.Connection
  if not con is nothing then exit sub

  con.ConnectionString="Provided=MSDAORA.1;user id=hr_db;password=hr_db;Datasource=ORA"  ' Datasource is the service name configure in TNSNAMES.ora file.

 con.Open

 rs.Open "select * from employee_master",con,adOpenDynamic
dim fld

text1.Text="List of field names in the table employee_master : "
if not rs.EOF
   for each fld in rs.Fields
     text1.text = text1.text & fld.Name & "," & vbcrlf
   next fld
end if
rs.close
con.close
set rs=nothing
set con=nothing
end sub