Friday, September 20, 2013

Chart Control ASP.Net

List all the .XLSM files from folder and sub folder - VBA Excel


Public Sub TestListDir()
    Worksheets(1).Cells(2, 1).Activate
    Call listDir("C:\temp\", 1)
End Sub

Public Sub listDir(strPath As String, lngSheet As Long)
Dim strFn As String
Dim strDirList() As String
Dim lngArrayMax, x As Long
lngArrayMax = 0
strFn = Dir(strPath & "*.*", 23)
While strFn <> ""
    If strFn <> "." And strFn <> ".." Then
        If (GetAttr(strPath & strFn) And vbDirectory) = vbDirectory Then
            lngArrayMax = lngArrayMax + 1
            ReDim Preserve strDirList(lngArrayMax)
            strDirList(lngArrayMax) = strPath & strFn & "\"
            'Debug.Print strDirList(lngArrayMax)
            
        Else
            ActiveCell.Value = strPath & strFn
             If InStr(strFn, ".xlsm") > 0 Then Debug.Print strPath & strFn
            'if(strFn.Name.)
            
            
            Worksheets(lngSheet).Cells(ActiveCell.Row + 1, 1).Activate
        End If
    End If
    strFn = Dir()
Wend
If lngArrayMax <> 0 Then
    For x = 1 To lngArrayMax
        Call listDir(strDirList(x), lngSheet)
    Next
End If
End Sub

Divide by zero SQL-server- NULLIF

Select qty/ NULLIF((select amt from amtfile where itemno=123),0) from itemqty


or user case when (select amt from amtfile where itemno=123) =0 then Null else select amt from amtfile where itemno=123) =0 end



Thursday, September 19, 2013

VBA code Export to a Folder


Public Sub ExportModules()
    Dim bExport As Boolean
    Dim wkbSource As Excel.Workbook
    Dim szSourceWorkbook As String
    Dim szExportPath As String
    Dim szFileName As String
    'Dim cmpComponent As VBIDE.VBComponent

    ''' The code modules will be exported in a folder named.
    ''' MFExport in the Documents folder.
    ''' The code below create this folder if it not exist
    ''' or delete all files in the folder if it exist.
    
    On Error Resume Next
    MkDir "MFExport"
    On Error GoTo 0

    
    If FolderWithMFExport = "Error" Then
        MsgBox "Export Folder not exist"
        Exit Sub
    End If
    
    On Error Resume Next
        Kill FolderWithMFExport & "\*.*"
        
'        Dim oldfolder As String
'        Dim Newfolder As String
'        oldfolder = "MFExport"
'        Newfolder = "MFExportOLD"
'        Name oldfolder As Newfolder
        
    On Error GoTo 0

    ''' NOTE: This workbook must be open in Excel.
    szSourceWorkbook = ActiveWorkbook.Name
    Set wkbSource = Application.Workbooks(szSourceWorkbook)
    
    If wkbSource.VBProject.Protection = 1 Then
    MsgBox "The VBA in this workbook is protected," & _
        "not possible to export the code"
    Exit Sub
    End If
    
    szExportPath = FolderWithMFExport & "\"
    
    For Each cmpComponent In wkbSource.VBProject.VBComponents
        
        bExport = True
        szFileName = cmpComponent.Name

        ''' Concatenate the correct filename for export.
        Select Case cmpComponent.Type
            Case vbext_ct_ClassModule
                szFileName = szFileName & ".cls"
            Case vbext_ct_MSForm
                szFileName = szFileName & ".frm"
            Case vbext_ct_StdModule
                szFileName = szFileName & ".bas"
            Case vbext_ct_Document
                ''' This is a worksheet or workbook object.
                ''' Don't try to export.
                bExport = False
        End Select
        
        If bExport Then
            ''' Export the component to a text file.
            cmpComponent.Export szExportPath & szFileName
            
        ''' remove it from the project if you want
        '''wkbSource.VBProject.VBComponents.Remove cmpComponent
        
        End If
   
    Next cmpComponent

 
    Dim OldFolderName As String, NewFolderName As String
    
    '-- Set Folder Names
    OldFolderName = szExportPath
    NewFolderName = Replace(szExportPath, "MFExport\", "MFExport") + "_" + Format(Now(), "yyyymmdd") + "_" + Format(Now(), "HHMMSS")
    
    On Error Resume Next
        Kill NewFolderName
    '-- Rename them
    Name OldFolderName As NewFolderName
        
        
        

    MsgBox "Export is ready"
End Sub


Public Sub ImportModules()
    Dim wkbTarget As Excel.Workbook
    Dim objFSO As Scripting.FileSystemObject
    Dim objFile As Scripting.File
    Dim szTargetWorkbook As String
    Dim szImportPath As String
    Dim szFileName As String
    Dim cmpComponents As VBIDE.VBComponents

    If ActiveWorkbook.Name = ThisWorkbook.Name Then
        MsgBox "Select another destination workbook" & _
        "Not possible to import in this workbook "
        Exit Sub
    End If

    'Get the path to the folder with modules
    If FolderWithMFExport = "Error" Then
        MsgBox "Import Folder not exist"
        Exit Sub
    End If

    ''' NOTE: This workbook must be open in Excel.
    szTargetWorkbook = ActiveWorkbook.Name
    Set wkbTarget = Application.Workbooks(szTargetWorkbook)
    
    If wkbTarget.VBProject.Protection = 1 Then
    MsgBox "The VBA in this workbook is protected," & _
        "not possible to Import the code"
    Exit Sub
    End If

    ''' NOTE: Path where the code modules are located.
    szImportPath = FolderWithMFExport & "\"
        
    Set objFSO = New Scripting.FileSystemObject
    If objFSO.GetFolder(szImportPath).Files.Count = 0 Then
       MsgBox "There are no files to import"
       Exit Sub
    End If

    'Delete all modules/Userforms from the ActiveWorkbook
    Call DeleteVBAModulesAndUserForms

    Set cmpComponents = wkbTarget.VBProject.VBComponents
    
    ''' Import all the code modules in the specified path
    ''' to the ActiveWorkbook.
    For Each objFile In objFSO.GetFolder(szImportPath).Files
    
        If (objFSO.GetExtensionName(objFile.Name) = "cls") Or _
            (objFSO.GetExtensionName(objFile.Name) = "frm") Or _
            (objFSO.GetExtensionName(objFile.Name) = "bas") Then
            cmpComponents.Import objFile.Path
        End If
        
    Next objFile
    
    MsgBox "Import is ready"
End Sub

Function FolderWithMFExport() As String
    Dim WshShell As Object
    Dim FSO As Object
    Dim SpecialPath As String

    Set WshShell = CreateObject("WScript.Shell")
    Set FSO = CreateObject("scripting.filesystemobject")

' Define file Path
    'SpecialPath = WshShell.SpecialFolders("MyDocuments")
    SpecialPath = WshShell.SpecialFolders("c:/")
    
'//SpecialPath = "c:/temp"
    If Right(SpecialPath, 1) <> "\" Then
        SpecialPath = SpecialPath & "\"
    End If
    
    If FSO.FolderExists(SpecialPath & "MFExport") = False Then
        On Error Resume Next
        MkDir SpecialPath & "MFExport_" + Date
        On Error GoTo 0
    End If
    
    If FSO.FolderExists(SpecialPath & "MFExport") = True Then
        FolderWithMFExport = SpecialPath & "MFExport"
    Else
        FolderWithMFExport = "Error"
    End If
    
End Function

Function DeleteVBAModulesAndUserForms()
        Dim VBProj As VBIDE.VBProject
        Dim VBComp As VBIDE.VBComponent
        
        Set VBProj = ActiveWorkbook.VBProject
        
        For Each VBComp In VBProj.VBComponents
            If VBComp.Type = vbext_ct_Document Then
                'Thisworkbook or worksheet module
                'We do nothing
            Else
                VBProj.VBComponents.Remove VBComp
            End If
        Next VBComp
End Function






Rename folder VBA Excel

  Dim OldFolderName As String
  Dim NewFolderName As String
    
    OldFolderName = "C:\testfloderNew"
    NewFolderName = "C:\testfolderOld"
    
    '-- Rename them
    Name OldFolderName As NewFolderName


Wednesday, September 18, 2013

C# 4.0 Graph with 2 Y axis asp.net

<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    <asp:Chart ID="Chart1" runat="server" DataSourceID="SqlDataSource1" 
        Width="643px">
        <series>
            <asp:Series Name="Series1" XValueMember="ObjSub" 
        YValueMembers="Period_04" ChartType="Line" YValuesPerPoint="2"  >
            </asp:Series>
        
        <asp:Series Name="Series2" XValueMember="ObjSub" 
        YValueMembers="Period_05" ChartType="Line" YValuesPerPoint="2">
            </asp:Series>

        
        </series>


        <chartareas>
            <asp:ChartArea Name="ChartArea1">
            </asp:ChartArea>
        </chartareas>
    </asp:Chart>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:BUDGETReports %>" 
        SelectCommand="SELECT top 5  [ObjSub], sum([Period_04]) Period_04, sum([Period_01]) Period_05 FROM [BudgetData] where fy=14 and co=1 group by objsub">
    </asp:SqlDataSource>
    </form>
</body>
</html>

Monday, September 16, 2013

Load Data into Dropdownlist using SQL -server (no sqldatasource)

void filDDPN()
{

    DropDownList1.Items.Clear();
    DataTable subjects = new DataTable();

    using (SqlConnection con = new SqlConnection(ConnectionString))
    {

        try
        {
            SqlDataAdapter adapter = new SqlDataAdapter("SELECT [Period] FROM [tbl500_Periods] order by id desc", con);
            adapter.Fill(subjects);

            DropDownList1.DataSource = subjects;
            DropDownList1.DataTextField = "Period";
            DropDownList1.DataValueField = "Period";
            DropDownList1.DataBind();
        }
        catch (Exception ex)
        {
            // Handle the error
        }

    }
}

Tuesday, September 10, 2013

Date format GirdView

  <asp:boundfield datafield="Your_Date_Column" dataformatstring="{0:MMMM d, yyyy}" htmlencode="false" />


Gridview item wrap fixed width

 
<asp:BoundField DataField="COMMTS" HeaderText="Comments" ItemStyle-Wrap="true" ItemStyle-Width="150"

SortExpression="COMMTS" />