Thursday, January 6, 2011

Exporting data from Access table / query to a delimited textfile

This is a real simple one (quick and dirty, no exception handling) making use of an InputBox to set the filename and path and then exporting it to a text file. I created the following function in VBA

Public Function ExportToTxt()
 Dim strPath As String
 Dim strDate As String
 Dim strFullPath As String

 ' User sets folder location    
 strPath = InputBox("Give path", "Export to", "J:\Outgoing\YYYYMMDD") & "\"


 ' User sets extract date    
 strDate = InputBox("Give date in YYYYMMDD", "Export to", "YYYYMMDD")

 ' Set filename including path in string, to be used with the Transfer function
 strFullPath = strPath & "\" & strDate
       
       
 ' Export results to a delimited textfile,
 '  - using specifications specifications named "BrochSpecs" and "TDSpecs"
 '  - from table/query "03 Export BR Extract" and "04 Export TD Extract"
 '  - to filename and location set in strFullpath, postfixed by "_BR_Extract.txt"
   


 DoCmd.TransferText acExportDelim, "BrochSpecs", "03 Export BR Extract", strFullPath & "_BR_Extract.txt", True
 DoCmd.TransferText acExportDelim, "TDSpecs", "04 Export TD Extract", strFullPath & "_TD_Extract.txt", True


End Function

Thursday, December 30, 2010

Fetching the value from a clicked cell in a DataGridView


// Use this method for the cell click event handler:
       
private void dataGridView1_CellClick( object sender, DataGridViewCellEventArgs e)
{
   int yCoord = dataGridView1.CurrentCellAddress.Y;
   int xCoord = dataGridView1.CurrentCellAddress.X;
   MessageBox.Show(dataGridView1.Rows[yCoord].Cells[xCoord].Value.ToString())
}

Wednesday, December 29, 2010

Using the Timer class

Great... I accidentally deleted all previous posts so I can start all over again.

Anyhow.

I'm trying to get my head around delegates and events and while doing this, i stumbled across the Timer class. What I tried to accomplish was to create a simple stop watch application to better understand the principles of delegates and events. After some studying, I noticed that I was mixing up the Stopwatch and Timer class. So this made me decide to focus solely on the Timer class for now. Also to keep the whole delegates and events happening really simple.

So, how can we define the Timer class? According to MSDN:

"Provides a mechanism for executing a method at specified intervals. This class cannot be inherited."

I don't think there is a more clear definition than that... which actually suprises me because a t times i feel reading the MSDN documentation is like reading Japanese. Ok, to put this in practice I created the following:

  • a simple form
  • a button called 'button1' (which is the default name for the first button you drag-n-drop), with the text 'Start'
  • an empty textbox, called 'txtShowTime'

This is how I set up the Timer (by code):

Go the the Form1.Designer.cs code, look for the following lines and add:

private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox txtShowTime;
private System.Windows.Forms.Timer tickTock; // add, declares a Timer variable

At the very top of the InitializeComponent() look for the following lines and add:

this.button1 = new System.Windows.Forms.Button();
this.txtShowTime = new System.Windows.Forms.TextBox();
this.SuspendLayout();
this.components = new System.ComponentModel.Container();
this.tickTock = new System.Windows.Forms.Timer(this.components); // add, initializes an instance of type Timer

Now, anywhere in the InitializeComponent(), add the following lines:

this.tickTock.Interval = 250; // sets the frequency  of the event in millisecs
this.tickTock.Enabled = false; // disabling the timer per default
this.tickTock.Tick += new System.EventHandler(tickTock_Tick); // creating event handler

At this point, we have to create a method to which the event handler is pointing to ('tickTock_Tick'). Create this in Form1.cs:

private void tickTock_Tick(object sender, EventArgs e)
{
   txtShowTime.Text = DateTime.Now.ToString(); // displays the time in the textbox
}

So, every 0,25 sec the tickTock_Tick method is called and will display/refresh the time in the textbox. 


To initialize the displaying of time on the form, we will add some functionality to the Button1. Look for it in Form1.Designer.cs and change as follows:

this.button1.Location = new System.Drawing.Point(13, 13);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Start";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click +=new System.EventHandler(button1_Click); // add this to create an event handler upon clicking the button

Same story here as for the Tick event; we need to create a method in Form1.cs named 'button1_Click'. In this method we'll add some simple functionality which will start the timer and change the text of the button. Alright, go to Form1.cs and add the following code right after the tickTock_Tick method:

private void button1_Click(object sender, EventArgs e)
{
if (tickTock.Enabled == true)
{
    tickTock.Enabled = false;
           button1.Text = "Go!";
}
else
{
       tickTock.Enabled = true;
       button1.Text = "Stop";
}
}


Run the app and test.