11.07.2015 Views

101 Tech Tips - Visual Studio Magazine - One-Stop Source Shop

101 Tech Tips - Visual Studio Magazine - One-Stop Source Shop

101 Tech Tips - Visual Studio Magazine - One-Stop Source Shop

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

For even more tricks and tips go towww.vbpj.com or www.vcdj.comm_CloseButton.Click +=new EventHandler(OnCloseButtonClick);Controls.Add(m_CloseButton);}protected void OnCloseButtonClick(object sender,EventArgs e){Close(); //calls Dispose() for you as well}}You can also double-click on the Close button in the visualdesigner and let <strong>Visual</strong> <strong>Studio</strong>.NET generate this code for you.In your Close button Click event handler, simply call your baseclass (System.Windows.Forms.Form) Close( ) method to closethe form. In addition to closing the form, the Form.Close( )implementation also calls Dispose( ) for you, so you don’t need tocall it explicitly yourself.Finally, you need to handle the event of the user hitting theEscape key. The convention in Windows is that this action shouldclose the form, just as if the user clicked on the Close button. Inyour form constructor, after the call to InitializeComponent( ), setyour base class CancelButton property to the Close button youjust added. This will redirect the Escape event to your button, asif the user clicked on it.—Juval Lowy, San Jose, Calif.author of COM+ Services - Mastering COM and .NETComponent Services [O’Reilly, 2001]VB6, SQL Server 6.5 and upLevel: IntermediateLet MTS Handle Transaction ManagementWhen you call a stored procedure in SQL Server that performs datamanipulation on the database from a Microsoft Transaction Server(MTS) transaction, let MTS handle all the transaction management.Don’t put a BEGIN TRANSACTION | COMMIT TRANSACTION |ROLLBACK TRANSACTION in the stored procedure. The transactionyou create in the stored procedure doesn’t enlist in the MTStransaction, so MTS isn’t notified when you handle the SQL errorsmanually. This means an error in your stored procedure won’tforce the rollback of the MTS transaction’s other parts. The MTStransaction returns a success notification even when part of thetransaction failed.VB4/32, VB5, VB6Level: Beginning—Jason Rein, Thompson’s Station, Tenn.Return File Version InfoRegarding the “Retrieve File Version Information” tip in the 11 thEdition of the “<strong>101</strong> <strong>Tech</strong> <strong>Tips</strong> for VB Developers” supplement[<strong>Visual</strong> Basic Programmer’s Journal March 2001], I have a shorterfunction that achieves the same task. To use the FileSystemObject,you need to reference the Microsoft Scripting Runtime:Public Function GetExecutableFileVersion(ByVal _Filename As String) As StringDim FileObj As Scripting.FileSystemObject' Create ObjectSet FileObj = New Scripting.FileSystemObjectIf FileObj.FileExists(Filename) ThenGetExecutableFileVersion = _FileObj.GetFileVersion(Filename)End If' Free ObjectSet FileObj = NothingEnd Function—Simon Murrell, Bedfordview, Gauteng, South AfricaVB4/32, VB5, VB6, VBSLevel: Intermediate✰✰✰✰✰ Five Star TipCreate ISAM Files Out of Thin Air<strong>Visual</strong> Basic Programmer’s Journal once published a tip on how touse undocumented Jet/SQL features to create a new ISAM file inthe format of your choice—such as Excel, dBase, Paradox, HTML,and Lotus—without having to use automation with the objectmodel or even having the destination file type’s application on theuser’s machine [“Export Data to ISAM Databases,” 6 th Edition of the“<strong>101</strong> <strong>Tech</strong> <strong>Tips</strong> for VB Developers” supplement, <strong>Visual</strong> BasicProgrammer’s Journal February 1998].Microsoft still says that method doesn’t exist. Here’s anotherit says doesn’t exist, but it does as of ADO/ADOX 2.1. Start a newVB project and add a reference to ADO and ADOX (Microsoft ADOExtensions for DDL and Security):Dim cn As ConnectionDim cat As CatalogDim tbl As TableDim fld As ColumnSet cn = New ConnectionWith cn.ConnectionString = _"Provider=Microsoft.Jet.OLEDB.4.0;" & _"Extended Properties=Excel 8.0;Data <strong>Source</strong>=" _& App.Path & "\anewfile.xls".OpenEnd WithSet cat = New CatalogWith cat.ActiveConnection = cnSet tbl = New Tabletbl.Name = "ANewSheet"Set fld = New Columnfld.Name = "MyCol1"fld.Type = adWCharfld.DefinedSize = 30tbl.Columns.Append fld.Tables.Append tblSet tbl = New Tabletbl.Name = "Another"Set fld = New Columnfld.Name = "Wubba"fld.Type = adWCharfld.DefinedSize = 10tbl.Columns.Append fld.Tables.Append tblcat.Tables.RefreshEnd WithSet fld = NothingSet tbl = NothingSet cat = Nothingcn.CloseSet cn = NothingRun it to see your Excel file created with a Worksheet namedNewSheet and a column named/typed as you specified.Use this to wow your users with multisheet reports. Once youcreate the sheets, you can use ADO to connect to the files andmanipulate them as you would any other ADO data source. But thecoolness of this technique lies not in the fact that you can manipulatean ISAM data source once you have one on your machine—that has always been easy. The coolness is the ability to create thefiles out of thin air in the first place using only ADO.Like the original SQL method, this approach creates mostother ISAMs too. Simply replace the Extended Properties= valuewith the specifier you desire, such as Extended Properties=dBaseIV;, Extended Properties=Paradox 4.x;, and so on.—Robert Smith, Kirkland, Wash.SEPTEMBER 2001 Supplement to <strong>Visual</strong> <strong>Studio</strong> <strong>Magazine</strong> 5

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!