Category Archives: Visual Basic

Data entry in MSHFlGrid control vb6


MSHFlex Grid[Visual Basic 6.0] control is a goog tool for showing data in a tabular format as in Excel.2018-08-31_221326

Data entry in  Grid also possible with a Text Box which require handling Keyup,KeyPress Events of the TextBox control.

Place a GridControl and a TextBox to your project and add the following code the Textbox Events.

Public Sub MCellEnter()
With MSHFlexGrid_POrder
txt_DEntry.Text = .TextMatrix(.row, .col)
txt_DEntry.Move (.CellLeft + .Left), (.CellTop + .Top), _
.CellWidth, .CellHeight
txt_DEntry.Visible = True
DoEvents
txt_DEntry.SetFocus
End With
End Sub

The above code place the text box on the active cell by adjusting hight, width and top of Text Box

Private Sub txt_DEntry_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
With MSHFlexGrid_POrder
.TextMatrix(.row, .col) = txt_DEntry
If .col < .Cols – 1 Then
.col = .col + 1
MCellEnter
Else
.col = 0
If (.row + 1) < .Rows Then
.row = .row + 1
End If
MCellEnter
End If

End With
End If

End Sub

The above code handle the enter keybehaviour and following keep track of Arrow key movements.

Private Sub txt_DEntry_KeyDown(KeyCode As Integer, Shift As Integer)
With MSHFlexGrid_POrder
.Refresh
.TextMatrix(.row, .col) = txt_DEntry

Select Case KeyCode
Case KeyCodeConstants.vbKeyDown
If .row + 1 < .Rows Then
.row = .row + 1
MCellEnter
End If
Case KeyCodeConstants.vbKeyUp
If (.row – 1) > 0 Then
.row = .row – 1
MCellEnter
End If
Case KeyCodeConstants.vbKeyRight
If (.col + 1 < .Cols) Then
.col = .col + 1
MCellEnter
End If
Case KeyCodeConstants.vbKeyLeft
If (.col – 1) >= 0 Then
.col = .col – 1
MCellEnter
End If
End Select
End With

End Sub

That’s all you need to know

Querying external database in vb6 DAO


DAO is one of many ways to access database in VB6. This tutorial tells you how you can access a .mdb database which is not pointed by the DAO Database object.

I have separate Access 2003 .mdb database files, one is pointed by the Database object conn and the other I want to access using the same connection.

SELECT * FROM [File Path][DBNAME].[TABLE]

Connection

Have a look at the connection. [May be you already know, what its look like, this for those not familiar with DAO in VB6]

Dim externalEmp As Recordset
Dim conn As Database
Set conn = OpenDatabase("D:\105443T.mdb")

Querying Records

The conn is now capable of querying any table within the 105443T database.

Say,

Set tr = conn.OpenRecordset("select * from Transactions")
If tr.RecordCount > 0 Then
 tr.MoveFirst
 tr.MoveNext
 tr.MoveFirst
 
 For c = 0 To tr.RecordCount - 1
 Debug.Print tr!date & "--" & tr!dr & "--" & tr!cr
 tr.MoveNext
 Next c
End If

Querying the external DB

Likewise, I can point external Database too with zero changes in conn object. Just specify the [path of file] [.] [data table Name] in place of the table name in the query.

Set externalEmp = conn.OpenRecordset("select * from C:\users\manoj\documents\office.emp")
If externalEmp.RecordCount > 0 Then
 externalEmp.MoveFirst
 externalEmp.MoveNext
 externalEmp.MoveFirst
 
 For c = 0 To externalEmp.RecordCount - 1
 Debug.Print externalEmp!Ename & "--" & externalEmp!edepart & "--" & externalEmp!ebasic
 externalEmp.MoveNext
 Next c
End If

The same is also possible with ADO and other connections.

That’s it.

How to get Computer Name using VB6 Code


Accessing system property is pretty easy with VB6 code. This can be useful when programmers need to refer the system name in the code.

Steps

  • Declare the GetComputerNameA function which resides in the kernel32 library.
  • Create function to fetch the Computer Name.
  • Use the function

The Kernel functions

Declare Function GetComputerNameA Lib “kernel32” (ByVal lpBuffer As String, nSize As Long) As Long

Get the computer name

Define a new function to do the rest of the task. So you can reuse the code.

Public Function GetComputerName() As String
Dim sResult As String * 255
GetComputerNameA sResult, 255
GetComputerName = Left$(sResult, InStr(sResult, Chr$(0)) - 1)

End Function

Calling the function

Just invoke the function and place values to a Text Box

Text1.Text = GetComputerName

VB6: List default printer using code in Windows


kernel32 hold lots of interesting function that you may not utilized yet. With the Profile String functionality programmers can list default printer name, with serial port, Driver etc. Is that interesting.

Just create a bunch of function and call it.

Declare the functions

Create the following functions and Alias which is the prerequisite for our program.

Declare Function GetProfileString Lib “kernel32.dll” Alias “GetProfileStringA” (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long
Define the GetDefaultPrinter function

Let create our custom functions for fetching the default printer.

Public Function GetDefaultPrinter() As Printer

Dim strBuffer As String * 254

Dim iRetValue As Long

Dim strDefaultPrinterInfo As String

Dim tblDefaultPrinterInfo() As String

Dim objPrinter As Printer

' Retreive current default printer information

iRetValue = GetProfileString("windows", "device", ",,,", strBuffer, 254)

strDefaultPrinterInfo = Left(strBuffer, InStr(strBuffer, Chr(0)) - 1)

tblDefaultPrinterInfo = Split(strDefaultPrinterInfo, ",")

For Each objPrinter In Printers

If objPrinter.DeviceName = tblDefaultPrinterInfo(0) Then

' Default printer found !

Exit For

End If

Next

' If not found, return nothing

If objPrinter.DeviceName <> tblDefaultPrinterInfo(0) Then

Set objPrinter = Nothing

End If

Set GetDefaultPrinter = objPrinter

End Function

Using the function to list default

Drag and drop a command button and a Text Box to your form and drop these code into click event of the button.

Private Sub Command8_Click()

Dim objPrinter As Printer

Set objPrinter = GetDefaultPrinter()

Text2.Text = "Default printer is: " + objPrinter.DeviceName & _

Chr(13) & " Driver name is: " + objPrinter.DriverName _

& Chr(13) & " Port is: " + objPrinter.Port

Set objPrinter = Nothing

End Sub

Lets run and see what going on

VB6: List all printers using code in Windows


Listing all printers with name, Port in VB6 is simple as listing default printer port using Kernel32 library . Today we going show you how to do this.

As usual we start with function declaration, then define a new function which add all information into a list box. Here we goes

Kernel32 GetProfileString method

Declare Function GetProfileString Lib “kernel32.dll” Alias “GetProfileStringA” (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long

Add printer information into a list box

Public Sub GetPrinterList(lstPrinter As ListBox)Dim PrintData As PrinterDim defprinterpos%

For Each PrintData In Printers

‘ Add printer name and port to list

lstPrinter.AddItem PrintData.DeviceName & ” at: ” & PrintData.Port

‘ Check for default printer

If PrintData.DeviceName = Printer.DeviceName Then defprinterpos = lstPrinter.NewIndex

Next

lstPrinter.ListIndex = defprinterpos%

End Sub

This sub procedure will take list box as argument, and add printer to this list box.

Call the sub

The final line of code just invoke the procedure and see what we have.

Happy coding

Create a bootable Windows USB in 5 minutes


Windows is my favourite OS since the Windows 7 edition. Usually we install most of OS from CD/DVD ROM. What about an USB or an SSD? You can do install Windows 7/8 from a boot able USB/Pen drive, but simply copying all CD contents to USB doesn’t work at all.

There are many programs which can reincarnate USB into bootable USB. But I suggest do it like an expert with some simple command.


First Step

Connect your device to a Windows installed system/PC also connect the USB want to use with a primary slot. We need at least 8 GB for installation purpose.

We are going to format the USB, make sure all the necessary data is backup from the storage device.

Preparing the USB with command prompt

Launch the command prompt with Admin privileges( Run -cmd and press enter) and  type c:/windows/system32 and issue the DISKPART command.  Now you have two command window.

Now list all disk partition with DISK LIST and identify your partition number, go to my computer and check volume size if had a confusion. The size will be 7534 MB for the 8 GB USB and I have disk with no 1.

Select the partition by issuing select disk [Disk #] command here it is SELECT DISK 1  for my computer, it may vary depending on the hard disks of PC you are using.

Now clean the disk. Issue CLEAN  command and make a new partition using CREATE PARTITION PRIMARY and make new partition active with ACTIVE command

Second Step

Need to format the newly created partition with the format command. FORMAT FS=NTFS QUICK. It will take some time to finish the process. Have a tea …. lol

Now we need two commands which let you go to the finalize step , ACTIVE and EXIT command (you will be automatically exit from the diskpart utility), don’t exit the command prompt.

Final Step

Go to your My computer and load the Windows installation DVD  and not the drive letter of DVD and US , suppose I have letter  ‘E’ for DVD and F  for USB.

Now the command prompt look like C:/Windows/System32>, OK ? lets make the bootable records follow these steps (issue following commands)

  1. E: CD BOOT and repeat CD BOOT until the prompt changed to E:/BOOT> (‘E’ stands for DVD Drive)
  2. Lets update USB Drive ‘F’ with BOOTMGR with compatible Bootcode, issue the BOOTSECT.EXE/NT60 F:(USB Drive).

If everything goes right( You will get the message ‘Bootcode was successfully updated on all targeted volumes’) , it will be OK for copying complete Windows (7/8/8.1)DVD content to the USB.

How to Boot

Choose  your BIOS feature to make your USB device booting device, the setting may vary depending on the different BIOS. Now you know how to install Windows for USB.

Connect the USB to a computer and restart , boot with USB…

Thank you for reading.

 

Add new fields to Access table using VB6


Let me show how to add a new field into an existing access database using Visual Basic 6.0.Using the tableDef and Filed object you can create new fields. Firstly, you need to create Database and Recordset object, mke sure the DAO access object library. Continue reading