Loading
Showing posts with label Visual Basic 6. Show all posts
Showing posts with label Visual Basic 6. Show all posts

Tuesday, May 18, 2010

Visual Basic 6 - How to Hide Application in Windows Task List

This code will hide your application in windows tasklist using visual basic when user press the ctr + alt + delete in keyboard. This type of program is very useful if you don't want your users/clients to close the application through windows tasklist. 

Here's the code sample below.

Declare Function SetWindowPos Lib "user32" (ByVal hwnd _
As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, _
ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal _
wFlags As Long) As Long

Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, ByVal _
lpWindowName As String) As Long

Const SWP_HIDEWINDOW = &H80
Const SWP_SHOWWINDOW = &H40
Private Sub Command1_Click()
Dim Thwnd as Long
Thwnd = FindWindow("Shell_traywnd", "")
Call SetWindowPos(Thwnd, 0, 0, 0, 0, 0, SWP_HIDEWINDOW)
End Sub

Private Sub Command2_Click()
Dim Thwnd as Long
Thwnd = FindWindow("Shell_traywnd", "")
Call SetWindowPos(Thwnd, 0, 0, 0, 0, 0, SWP_SHOWWINDOW)
End Sub

How to Create a Hot Key In Visual Basic 6

What is a Hot Key Control
A hot key is a key combination that the user can press to perform an action quickly. For example, a user can create a hot key that activates a given window and brings it to the top of the z-order. The hot key control displays the user's choices and ensures that the user selects a valid key combination. Here's the code below on how to create a hot key control.

Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Long) As Long

Declare Function DefWindowProc Lib "user32" _
Alias "DefWindowProcA" (ByVal hwnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long

Public Const WM_SETHOTKEY = &H32
Public Const WM_SHOWWINDOW = &H18
Public Const HK_SHIFTA = &H141 'Shift + A
Public Const HK_SHIFTB = &H142 'Shift * B
Public Const HK_CONTROLA = &H241 'Control + A
Public Const HK_ALTZ = &H45A

'The value of the key-combination has to
'declared in lowbyte/highbyte-format
'That means as a hex-number: the last two
'characters specify the lowbyte (e.g.: 41 = a),
'the first the highbyte (e.g.: 01 = 1 = Shift)

Private Sub Form_Load()
Me.WindowState = vbMinimized

'Let windows know what hotkey you want for
'your app, setting of lParam has no effect

erg& = SendMessage(Me.hwnd, WM_SETHOTKEY, _
HK_ALTZ, 0)

'Check if succesfull
If erg& <> 1 Then
MsgBox "You need another hotkey", vbOKOnly, _
"Error"
End If

'Tell windows what it should do, when the hotkey
'is pressed -> show the window!
'The setting of wParam and lParam has no effect
erg& = DefWindowProc(Me.hwnd, WM_SHOWWINDOW, _
0, 0)

End Sub

Tuesday, April 27, 2010

System API Programming


Visual Basic 6 - API Programming on how to get computer name

'Get Computer Name
Declare Function SetComputerName Lib "kernel32" _
Alias "SetComputerNameA" (ByVal lpComputerName As String) As Long


Private Sub cmdChange_Click()
SetComputerName txtNewName.Text
End Sub


' Check if program running
Private Sub Form_Load()


'// Not the best way to check
'// Better to use the FindWindow API


If App.PrevInstance = True Then
MsgBox ("This program is already running.")
End
End If


End Sub

Wednesday, April 7, 2010

Visual Basic - Synchronize Client Computer Date/Time From a SQL Server

The code below is used to synchronize the client computer from MS SQL Server, sometimes the users  may change their computer local time in-order to trick the system that he/she encoded the data entry on time. Oh, if you're the database administrator or programmer it's a very headache how to figure it out, why it is happen this way. Sample scenario, accounts receivable should be encoded prior to cut-off period, then user name "Michelle" forgot to encode the data, a day after cut-off period she realize encode the accounts receivable data, she might change the local time in-order to encode in previous dates. Basically, visual basic program compares the entry date (date picker component) and the system/local time, if she change the system date less than current date, you might wonder why the data not posted in ledger to think that all entries are encoded on timely basis. So, how to prevent it. Use the code below and don't forget to create a stored procedure called "Get_Server_Date". Call this function as many as you can for example during Loan Event, Add New Entry, Edit Entry, Save Entry, Delete Entry. Anyway it's very fast, the user can't recognize that you're synchronizing from the server time.
Note: I assume the all your data tables has these two or three fields
User_Name - the one who transact the data
DateEncoded/Modified  - the transaction date base on user's local time.
Computer Name - Optional, it use to trace the users location.
If it's not added to your data table, please do apply the fields it may help you trace your transactions.

'------ VB Code ----------
'Declaration
Dim Const mCon="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Mydatabase;Data Source=MyComputerName\SQLEXPRESS"



'Note : Change the Mydatabase to your Database Name
 '            MyComputerNmae = Your Database Server Name





Private Sub Form_Load()




       Call SynChronize_Date_Server()

End Sub



Function SynChronize_Date_Server()
On Error Resume Next
    Dim tmp1 As Date
    With Main.ado1   ' Add adodc1 object to the form and name it ado1
        .ConnectionString = mCon
        .CommandType = adCmdStoredProc
        .RecordSource = "sp_getserverdate;1"
        .Refresh
        Date = Format(.Recordset!ServerDate, "mm/dd/yy")
        Time = Format(.Recordset!ServerDate, "hh:mm:ss")
    End With
End Function

Function Get_Server_Date() As Date

On Error Resume Next
    Dim tmp1 As Date
    With Main.ado1
        .ConnectionString = mCon
        .CommandType = adCmdStoredProc
        .RecordSource = "sp_getserverdate;1"
        .Refresh
        Get_Server_Date = Format(.Recordset!ServerDate, "mm/dd/yy")
    End With
End Function
'-------------------------------- STORED PROCEDURED CODE ----------------------------


USE [Inventry]
GO
/****** Object:  StoredProcedure [dbo].[sp_getserverdate]    

Script Date: 04/20/2010 01:19:56 ******/

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER  proc [dbo].[sp_getserverdate]
as
set nocount on
select getdate() as ServerDate




















How to Get Computer Name (Visual Basic 6)

Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Function Get_CompName() As String
    ' Returns the name of the local computer.
    Dim BUFFER As String * 512, length As Long
    length = Len(BUFFER)
    If GetComputerName(BUFFER, length) Then
        ' this API returns non-zero if successful,
        ' and modifies the length argument
        Get_CompName = Left(BUFFER, length)
    End If
End Function

How to View Crystal Report In Visual Basic 6


This sample code view the crystal report file (Employee.rpt) , please don't forget to add the Crystal Report Component or you got an error.

Private Sub cmdEmployee_Click()
On Error GoTo errHandler
    If Right(App.Path, 1) = "\" Then
        rep1.ReportFileName = App.Path + "Employee.rpt"
    Else
        rep1.ReportFileName = App.Path + "\" + "Employee.rpt"
    End If
    rep1.Action = 1
    Exit Sub
errHandler:
    MsgBox Str(Err.Number) + " " + Err.Description
End Sub