Tuesday, June 28, 2005

To open file from common dialogue control in VB

Code to open file from common dialogue control in VB

Dim PlaceToSave As String
Dim strTemp As String
CommonDialog1.DialogTitle = "Open Task flow model..."
CommonDialog1.Filter = "Excel File (*.xls)*.xls"
CommonDialog1.ShowOpen
g_txt_taskflowmodel.Text = CommonDialog1.FileName


Comments Pls
Credits : Shreejith G

To Print form the MSflexgrid in VB

Code to print form the flexgrid in VB

Dim d As Single

Printer.ScaleMode = vbTwips
d = CSng(Printer.ScaleWidth) / CSng(Me.MSFlexGrid1.Width)
Printer.Zoom = CInt(d * 100)
Printer.PaperSize = vbPRPSA3
'Printer.Orientation = vbPRORLandscape

Printer.PaintPicture MSFlexGrid1.Picture, 0, 0
Printer.EndDoc


Comments pls
Credits: Shreejith G

To add Hyperlink in VB6.0

Check out the code to get hyperlink features in Visual Basic

'To make this hyperlink look really professional
'Place the lblURL into a frame
'Re-size the frame so that it is exactly the SAME size
'as lblURL. Then in design time, change the mouse pointer
'for the form to 3 I-Beam. And change the MousePointer
'for the frame to 1 Arrow.
'As stated on the previous article if linking to e-mail
'then uncomment line containing mail and comment
'line below


Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Private Sub Form_Load()
With lblurl
.ForeColor = vbBlue
.Font.Underline = True
End With

End Sub

Private Sub lblurl_Click()
With lblurl
' Call ShellExecute(0&, vbNullString, "Mailto:" & .Caption, vbNullString, vbNullString, vbNormalFocus)
Call ShellExecute(0&, vbNullString, .Caption, vbNullString, vbNullString, vbNormalFocus)
End With
End Sub


Comments Pls
Credits: Shreejith G

Email with VB

Code to add email features in VB

Dim objOutlook As Object
Dim objOutlookMsg As Object
Set objOutlook = CreateObject("Outlook.Application")
Set objOutlookMsg = objOutlook.CreateItem(0)
If Text1.Text <> "" Then
With objOutlookMsg
.To = Text1
'.Cc = "ifneed@copy.com"
.Subject = "checking 1 2 4 "
.Body = "This is the body of message"
.HTMLBody = "HTML version of message"
.Attachments.Add ("c:\Fastcartaskflow1.xls")
.send 'Let´s go!
MsgBox "Mail has been sent successfully", vbInformation, Mail
End With
Else
MsgBox "Please enter the Email Id", vbCritical, Email
End If
Set objOutlookMsg = Nothing
Set objOutlook = Nothing


Comments Pls
Credits: Shreejith G

Monday, March 28, 2005

To transfer data from Excel sheet to MSflexgrid in VB6.0

Code to transfer data from excel to Msflexgrid in Vb
Dim xlBook As Excel.Workbook

Dim xlSheet As Excel.Worksheet

Dim fso As New FileSystemObject

Dim strxlsFileName As String
Dim i_RowCount As Integer, i_CurrentRow As Integer

Dim i_ColCount As Integer, i_CurrentCol As Integer


If g_txt_taskflowmodel.Text <> "" Then

strxlsFileName = g_txt_taskflowmodel


If Not fso.FileExists(strxlsFileName) Then



' Exit Sub



End If



Set xlApp = New Excel.Application



Set xlBook = xlApp.Workbooks.Open(strxlsFileName)



Set xlSheet = xlBook.Sheets(1)



' i_RowCount = xlSheet.Rows.Count

i_CurrentRow = 1
Do
'While i_CurrentRow <= i_RowCount

MSFlexGrid1.Rows = i_CurrentRow
MSFlexGrid1.Row = i_CurrentRow - 1
i_CurrentCol = 1

If xlSheet.Cells(i_CurrentRow, 3) = "" Then
Exit Do
End If
Do
' While i_CurrentCol <= i_ColCount
If xlSheet.Cells(1, i_CurrentCol) = "" Then
Exit Do
End If

MSFlexGrid1.Col = i_CurrentCol - 1

MSFlexGrid1.Text = xlSheet.Cells(i_CurrentRow, i_CurrentCol)
i_CurrentCol = i_CurrentCol + 1

Loop



i_CurrentRow = i_CurrentRow + 1

Loop
Set xlSheet = Nothing

Set xlBook = Nothing

xlApp.Workbooks.Close

Set xlApp = Nothing

Else


MsgBox "Please enter the path of the Excel File", vbCritical, Excelfile

' On Error Resume Next
End If
Comments pls
Credits: Shreejith G

Migrating msflexgrid data to excel in VB6.0

Check out the Code : Language: VB

Public Sub FlexGrid_To_Excel(TheFlexgrid As MSFlexGrid, _
TheRows As Integer, TheCols As Integer, _
Optional GridStyle As Integer = 1, Optional WorkSheetName _
As String)

Dim objXL As New Excel.Application
Dim wbXL As New Excel.Workbook
Dim wsXL As New Excel.Worksheet
Dim intRow As Integer ' counter
Dim intCol As Integer ' counter

If Not IsObject(objXL) Then
MsgBox "You need Microsoft Excel to use this function", _
vbExclamation, "Print to Excel"
Exit Sub
End If

'On Error Resume Next is necessary because
'someone may pass more rows
'or columns than the flexgrid has

'you can instead check for this,
'or rewrite the function so that
'it exports all non-fixed cells
'to Excel

On Error Resume Next

' open Excel
objXL.Visible = True
Set wbXL = objXL.Workbooks.Add
Set wsXL = objXL.ActiveSheet

' name the worksheet
With wsXL
If Not WorkSheetName = "" Then
.Name = WorkSheetName
End If
End With

' fill worksheet
For intRow = 1 To TheRows
For intCol = 1 To TheCols
With TheFlexgrid
wsXL.Cells(intRow, intCol).Value = _
.TextMatrix(intRow - 1, intCol - 1) & " "
End With
Next
Next

' format the look
For intCol = 1 To TheCols
wsXL.Columns(intCol).AutoFit
'wsXL.Columns(intCol).AutoFormat (1)
wsXL.Range("a1", Right(wsXL.Columns(TheCols).AddressLocal, _
1) & TheRows).AutoFormat GridStyle
Next

End Sub


Comments Pls
Credits : Shreejith G

Edit data in the msflexgrid cells in VB6.0

Check out.


Q)

I have a msflexgrid control. I want the user to input some data. But I when I click the cell, it won't put itself in edit mode. How do u edit these cells?

A)

I have no idea why Microsoft has not added direct editing to the FlexGrid Control; I guess it has been designed for read only, databinding. Anyway, I have posted the standard work around that I always use, it is a bit long, but it works great.

MSFlexGrid does not have a built-in cell editing capability, but it provides the hooks to make it easy for you to add that capability programmatically. The advantage of this approach is that you can tailor editing behavior to your taste. The basic technique involves smoke and mirrors: the editing occurs not in MSFlexGrid at all, but in a standard Textbox control that is positioned precisely over the cell being edited.

In this example, we will give the user two ways to get into the edit mode, either by double-clicking on a cell, or by simply starting to type in the current cell. The following two routines implement this:

Private Sub MSFlexGrid1_DblClick()
GridEdit Asc(" ")
End Sub

Private Sub MSFlexGrid1_KeyPress(KeyAscii As Integer)
GridEdit KeyAscii
End Sub

In each case we call a grid edit subroutine and pass it a keystroke. In the case of double-clicking, we pass the space character as a flag. The GridEdit routine initializes the edit box and moves it into position:

Sub GridEdit(KeyAscii As Integer)

'use correct font
Text1.FontName = MSFlexGrid1.FontName
Text1.FontSize = MSFlexGrid1.FontSize

Select Case KeyAscii
Case 0 To Asc(" ")
Text1 = MSFlexGrid1
Text1.SelStart = 1000
Case Else
Text1 = Chr(KeyAscii)
Text1.SelStart = 1
End Select
'position the edit box
Text1.Left = MSFlexGrid1.CellLeft + MSFlexGrid1.Left
Text1.Top = MSFlexGrid1.CellTop + MSFlexGrid1.Top
Text1.Width = MSFlexGrid1.CellWidth
Text1.Height = MSFlexGrid1.CellHeight
Text1.Visible = True
Text1.SetFocus
End Sub

For demonstration purposes, the Case statement in the GridEdit routine shows two different behaviors when entering the edit mode. In practice you would probably only use one of them, or a different one of your own creation. If the edit mode is entered by virtue of a double-click or a control key press, we copy the contents of the grid cell to the exit box and place the cursor at the end of the string. If the edit mode is entered by pressing a normal key, we ignore the original cell contents and insert the pressed key into the edit box. The positioning of the exit box could be done on one line with the Move method. Here we have used four lines so that it reads more easily in this article. Notice that MSFlexGrid conveniently gives us all the coordinate information we need.

Next, we need a couple of routines that handle housekeeping when the user moves to a different cell or moves focus back to the grid from another control. The LeaveCell event is also the place where you would put any data validation code that might be applicable.

Private Sub MSFlexGrid1_LeaveCell()
If Text1.Visible Then
MSFlexGrid1 = Text1
Text1.Visible = False
End If
End Sub

Private Sub MSFlexGrid1_GotFocus()
If Text1.Visible Then
MSFlexGrid1 = Text1
Text1.Visible = False
End If
End Sub

Next we place some navigation code in the KeyDown event of the edit box so that, for instance, the user can leave the edit mode by pressing ESC, and move to a different row by pressing an arrow key:

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyEscape
Text1.Visible = False
MSFlexGrid1.SetFocus
Case vbKeyReturn
MSFlexGrid1.SetFocus
Case vbKeyDown
MSFlexGrid1.SetFocus
DoEvents
If MSFlexGrid1.Row < MSFlexGrid1.Rows - 1 Then
MSFlexGrid1.Row = MSFlexGrid1.Row + 1
End If
Case vbKeyUp
MSFlexGrid1.SetFocus
DoEvents
If MSFlexGrid1.Row > MSFlexGrid1.FixedRows Then
MSFlexGrid1.Row = MSFlexGrid1.Row - 1
End If
End Select
End Sub

Finally we need a line of code to suppress the Beep that occurs when ENTER is pressed in a Textbox:

Private Sub Text1_KeyPress(KeyAscii As Integer)
'noise suppression
If KeyAscii = vbKeyReturn Then KeyAscii = 0
End Sub


In order for the edit box to merge seamlessly into the grid, you need to set several Textbox properties at design-time: set Appearance = 0 (flat), and BorderStyle = 0 (none). Also set Visible = False so that the edit box is not initially visible. To really fine-tune this code, the edit box needs a slight additional offset to the southeast (with a corresponding reduction in size) so that the text in it lines up exactly with the text in the cell beneath. You would probably also want to write some code behind the scroll event of the grid since clicking on the grid's scroll bar will not cause the edit box to loose focus.

Note that this technique is not limited to using a Textbox as your edit box. You could modify the sample code to use a ComboBox, a CheckBox, or even a calendar control for editing, based on the column being edited.

MSFlexGrid is a very flexible control indeed, and this article just touches on some of the things you can do with it. As you gain familiarity with it, it will become a more regular part of your toolbox. Cell merging and pivoting are two more unique features of the MSFlexGrid that give it tremendous power and bear investigation.
Comments pls

Credits: Shreejith G

Browse for a folder in VB6.0

Technology : VB 6.0 Language: Visual Basic

Start a new Visual Basic standard-EXE project and add a command button.

Type in the following code

Option Explicit

Private Const BIF_RETURNONLYFSDIRS = 1
Private Const BIF_DONTGOBELOWDOMAIN = 2
Private Const MAX_PATH = 260

Private Declare Function SHBrowseForFolder Lib _
"shell32" (lpbi As BrowseInfo) As Long

Private Declare Function SHGetPathFromIDList Lib _
"shell32" (ByVal pidList As Long, ByVal lpBuffer _
As String) As Long

Private Declare Function lstrcat Lib "kernel32" _
Alias "lstrcatA" (ByVal lpString1 As String, ByVal _
lpString2 As String) As Long

Private Type BrowseInfo
hWndOwner As Long
pIDLRoot As Long
pszDisplayName As Long
lpszTitle As Long
ulFlags As Long
lpfnCallback As Long
lParam As Long
iImage As Long
End Type


Private Sub Command1_Click()
'Opens a Browse Folders Dialog Box that displays the
'directories in your computer
Dim lpIDList As Long ' Declare Varibles
Dim sBuffer As String
Dim szTitle As String
Dim tBrowseInfo As BrowseInfo

szTitle = "Hello World. Click on a directory and " & _
"it's path will be displayed in a message box"
' Text to appear in the the gray area under the title bar
' telling you what to do

With tBrowseInfo
.hWndOwner = Me.hWnd ' Owner Form
.lpszTitle = lstrcat(szTitle, "")
.ulFlags = BIF_RETURNONLYFSDIRS + BIF_DONTGOBELOWDOMAIN
End With

lpIDList = SHBrowseForFolder(tBrowseInfo)

If (lpIDList) Then
sBuffer = Space(MAX_PATH)
SHGetPathFromIDList lpIDList, sBuffer
sBuffer = Left(sBuffer, InStr(sBuffer, vbNullChar) - 1)
MsgBox sBuffer
End If

End Sub
Run the program, and click on the command button. The browse for folder dialog will be displayed. Click on a directory and click on OK and the path you have selected will be shown in a message box. Commens Pls..

Credits: Shreejith G