Tuesday, July 08, 2008

A quick application to delete files older then a set number of days (or any interval).

Module Module1

    Sub Main()

        Fdelete()

    End Sub

    Private Sub Fdelete()

        ' Set directory to delete files

        Dim myDir As String = "C:\MyFiles\"

        Dim mydate As Date = Now

        For Each S As String In IO.Directory.GetFiles(myDir)

            If DateDiff( _

                    DateInterval.Day, _

                    IO.File.GetCreationTime(S), _

                    mydate) >= 7 Then

                ' Change the 7 to change the number of days to keep.

                ' Anything older then 7 days will be deleted. Also

                ' the date interval can be changed to whatever is

                ' appropriate for your needs.

                Try

                    IO.File.Delete(S)

                Catch ex As Exception

                    Console.WriteLine(ex.Message)

                End Try

            End If

        Next

    End Sub

End Module


posted on Wednesday, July 09, 2008 2:36:59 AM (Eastern Daylight Time, UTC-04:00)  #   
 Saturday, May 24, 2008

If you have ever wanted to alternate the rows in a Crystal Report but didn't know how to do so here is an example. Just paste this code into the Background Color using the Section Expert for the details section.

If int(RecordNumber/1)/2 = int(int(RecordNumber/1)/2) then
    Color (203, 255, 151)   
Else
    crWhite
  

The results will look like this.


posted on Saturday, May 24, 2008 1:22:08 PM (Eastern Daylight Time, UTC-04:00)  #   
 Sunday, October 14, 2007

Here is a quick sample showing how to set the back color on multiple text boxes when the cursor enters the text box and then setting it back to white when the cursor leaves it.

Public Class Form1

    Private Sub SetColor_Enter(ByVal sender As Object, _

    ByVal e As System.EventArgs) Handles _

        TextBox1.Enter, _

        TextBox2.Enter, _

        TextBox3.Enter, _

        TextBox4.Enter

        DirectCast(sender, Control).BackColor = Color.Thistle

    End Sub

    Private Sub SetColor_Leave(ByVal sender As Object, _

    ByVal e As System.EventArgs) Handles _

        TextBox1.Leave, _

        TextBox2.Leave, _

        TextBox3.Leave, _

        TextBox4.Leave

        DirectCast(sender, Control).BackColor = Color.White

    End Sub

 

End Class


posted on Monday, October 15, 2007 1:11:59 AM (Eastern Daylight Time, UTC-04:00)  #   
 Friday, September 07, 2007

If you have ever wanted to send the results of a query directly to Excel without having to convert from a rpt or CSV file here is a handy little tip. Export data from SQL Server to Excel without using SSIS or DTS.


posted on Saturday, September 08, 2007 12:40:33 AM (Eastern Daylight Time, UTC-04:00)  #   
 Wednesday, September 05, 2007

Recently I created an Inventory Adjustment program, which loaded the entire inventory into a datagridview control from a table, allowed the user to edit the numbers and then save it back to the table. The specs required that new records not be added through this adjustment program but up until today I couldn't find anyway to prevent what I thought was something we just had to live with when using the DGV.

Not being able to prevent this "New" record was causing the application to throw an error every time someone moved beyond the last record and inserted a value into one of the fields in the New Record. So I added it to my list of things to figure out later list and moved on. Then today, coincidently I was reading the Winforms post from Tim Mackley's Mostly Net blog and rand across this, which is where I learned about  .DefaultView.AllowNew = False.

So I could take this

And make it this

Which fixes the problem just fine.

        Dim da As SqlDataAdapter

        da = New SqlDataAdapter("Select * From tblInventory", Con)

        da.TableMappings.Add("Table", "Inventory")

        ds = New DataSet

        Try

            da.Fill(ds)

            ds.Tables("Inventory").DefaultView.AllowNew = False

            dgvInventory.DataSource = ds.Tables("Inventory")

 

        Catch ex As Exception

            MessageBox.Show( _

                ex.Message & vbNewLine & _

                ex.Source, "Data Error", MessageBoxButtons.OK)

        Finally

            da.Dispose()

            ds.Dispose()

        End Try

    End Sub


posted on Thursday, September 06, 2007 1:49:12 AM (Eastern Daylight Time, UTC-04:00)  #   
 Wednesday, August 01, 2007

Until recently I wrote all my SQL in my code, which was ok for most of the applications I build because I mostly write small Office Interop applications used by one or two people who have access to all the datastores anyway. Usually an Excel file or Access database.

Lately I have been writing more and more applications that use MS SQL Server as a datastore and require more then just basic security to protect the data. So I have been taking the time to learn to write and use stored procedures.

In the current application I am working on there are several stored procedures that don't return any information to the user. Their purpose is to clear and build tables as they are needed in the life of the application.  When looking for some sample code to copy from I couldn't find anything that showed me how to just run a stored procedure from code without the expectation of getting something back from the database.

So after some experimentation and some help from the Internet this is what I came up with.

 

    Private Sub btnFillTable_Click( _

        ByVal sender As System.Object, _

        ByVal e As System.EventArgs) Handles btnFillTable.Click

 

        ' Insert the stored procedure name

        ' and then fire it off with this.

        ExecuteSP("sp_InsertInv")

 

    End Sub

 

    Private Sub ExecuteSP(ByVal SpName As String)

        Dim cn As SqlConnection = New SqlConnection

        cn.ConnectionString = Con ' Set Con to equal your

        ' connections string.

        cn.Open()

        Dim cmd As SqlCommand = New SqlCommand(SpName, cn)

        cmd.CommandType = CommandType.StoredProcedure

        Try

            cmd.ExecuteNonQuery()

        Catch ex As Exception

            MessageBox.Show(ex.Message & vbNewLine & _

            ex.Source, "Data Error", MessageBoxButtons.OK)

        End Try

    End Sub


posted on Wednesday, August 01, 2007 5:19:16 PM (Eastern Daylight Time, UTC-04:00)  #   
 Tuesday, July 17, 2007

Comments are included in the code and the XML file I used for this example will be pasted to the end of this article.

Imports System.IO

Imports Microsoft.Office.Interop

 

Public Class frmMain

    Private Sub btnRunSample_Click( _

        ByVal sender As System.Object, _

        ByVal e As System.EventArgs) Handles btnRunSample.Click

 

        ' Use CreateDataset to fire off this function.

        ' The two arguments are the Path and file name

        ' for the export and the path and file name for

        ' the XML file to import.

 

        ' CreateDataset("File to export", "File to Import")

 

        CreateDataset("C:\CustomerList.xls", "C:\MyXML.xml")

 

    End Sub

 

    Private Sub CreateDataset( _

        ByVal strExportPath As String, _

        ByVal strXMLImport As String)

 

        ' I am using an XML file for my

        ' sample but you can use any dataset

        ' from any number of sources.

 

        Dim dS As New DataSet

        Dim fs As FileStream = New FileStream( _

            strXMLImport, FileMode.Open)

        Try

            dS.ReadXml(fs)

            dgvSample.DataSource = dS

            dgvSample.DataMember = "CustomerList"

        Catch ex As Exception

            MessageBox.Show(ex.Message & vbNewLine & ex.Source, _

                "Error", MessageBoxButtons.OK)

        Finally

            fs.Close()

        End Try

        Export2Excel(strExportPath, dS.Tables(0))

    End Sub

 

    Private Sub Export2Excel( _

        ByVal Filename As String, _

        ByVal dt As DataTable)

 

        ' I can't take credit for this section of code

        ' since tt was cut, pasted and modified from some

        ' code I lifted from http://www.vbforums.com.

        ' I looked for the original code posted so I could

        ' credit the author but I couldn't find it anymore.

 

        ' This codes works fine for Excel 2003 and can be

        ' modified slightly to work with XP. Remember to

        ' add the correct Reference Library to your project.

 

        Dim excel As New Excel.ApplicationClass()

        Dim wBook As Excel.Workbook = Nothing

        Dim wSheet As Excel.Worksheet = Nothing

        Try

            wBook = excel.Workbooks.Add()

            wSheet = CType(wBook.ActiveSheet(), Excel.Worksheet)

            wSheet.Name = "News"

            Dim dc As System.Data.DataColumn

            Dim dr As System.Data.DataRow

            Dim colIndex As Integer = 0

            Dim rowIndex As Integer = 0

            For Each dc In dt.Columns

                colIndex = colIndex + 1

                excel.Cells(1, colIndex) = dc.ColumnName

            Next

            For Each dr In dt.Rows

                rowIndex = rowIndex + 1

                colIndex = 0

                For Each dc In dt.Columns

                    colIndex = colIndex + 1

                    excel.Cells(rowIndex + 1, colIndex) = _

                    dr(dc.ColumnName)

                Next

            Next

            wSheet.Columns.AutoFit()

            wBook.SaveAs(Filename)

        Catch ex As Exception

            MessageBox.Show(ex.Message & vbNewLine & ex.Source, _

                "Error", MessageBoxButtons.OK)

        Finally

            If Not wBook Is Nothing Then

                wBook.Close()

            End If

            If Not excel Is Nothing Then

                excel.Quit()

            End If

        End Try

    End Sub

End Class

 

*************************************************

*************************************************

*************************************************

 

Here is what the myXML.xml

 

<?xml version="1.0" encoding="utf-8"?>

<NewDataSet>

  <CustomerList>

    <FullName>Tom Servo</FullName>

    <AddressLine1>100 Ann St</AddressLine1>

    <City>Somewhere</City>

    <State>MI</State>

    <ZipCode>49094</ZipCode>

  </CustomerList>

  <CustomerList>

    <FullName>Auther Dent</FullName>

    <AddressLine1>555 Maple St</AddressLine1>

    <City>Nowhere</City>

    <State>MI</State>

    <ZipCode>49009</ZipCode>

  </CustomerList>

  <CustomerList>

    <FullName>Rand Flagg</FullName>

    <AddressLine1>207 Walking Dude</AddressLine1>

    <City>Anywhere</City>

    <State>MI</State>

    <ZipCode>49001</ZipCode>

  </CustomerList>

</NewDataSet>

 

 


posted on Tuesday, July 17, 2007 5:00:01 PM (Eastern Daylight Time, UTC-04:00)  #   
 Friday, July 13, 2007

Over the last year I have shed large amounts of blood, sweat and tears  in my struggle to master Crystal Reports starting with Version 9 and now currently Version 11. Don't get me wrong. It's an extremely powerful reporting tool and I don't think there is any other reporting tool that even comes close to its abilities. However, I am almost always disappointed with the built in Help (maybe the worst of any professional development tool, at least, that I have come in contact with) and with the lack of learning resources that can be found on the Internet.   

So for the most part when I run into a situation that I can't figure out in CR I have to turn to a lot of trial and error sprinkled with as much useful information I can find on the Internet. Well the other day I was wondering to myself (again) why it is that Crystal Reports doesn't include a built in Spell Checker. Something that you would think would be a natural feature for a reporting tool. Well its not so I usually end up doing a copy / paste to Word and back to achieve a spell check for my reports. Anyway, at the time, I thought there had to be others who have needed this feature and that someone must have built a third party tool that could accomplish a spell check without the extra steps of cutting and pasting from Word.

So I Google Spell Check in Crystal Reports and found this wonderful resource for Crystal Reports. I am not sure how I have never run across before but I hadn't. It is called the Crystal Reports Underground and the site includes a Blog here. The Blog is a must read for anyone working with Crystal Reports.

By the way, from that site, I found that there is a developer working on a Spell Checker and needs beta testers.


posted on Friday, July 13, 2007 4:47:42 PM (Eastern Daylight Time, UTC-04:00)  #