Jul 09

Insert Record/Data that Contain Quotes in Visual Basic

‘This visual basic shows how to use ADO to insert records into a database when fields contain quotes

‘The program creates a SQL INSERT statement. To protect against quotes in the
‘user-entered values, the program replaces each single quote with two single
‘quotes. The database replaces the pairs of quotes with single quotes when it
‘inserts the values. For example, the database treats the text “Tess’s Bakery” as
‘”Tess’s ” when it adds it to the database.

Private Sub cmdInsert_Click()
Dim DB_Name As String
Dim SQL_Str As String
Dim Conn As ADODB.Connection
Dim ctl As Control
‘ Get the data.
DB_Name = App.Path
If Right$(DB_Name, 1) <> “\” Then DB_Name = DB_Name & “\”
DB_Name = DB_Name & “Employee.mdb”
‘ Open a connection.
Set Conn = New ADODB.Connection
Conn.ConnectionString = “Provider=Microsoft.Jet.OLEDB.4.0;” & “Data Source=” & DB_Name & _
“;” & “Persist Security Info=False”
Conn.Open
‘ Compose the INSERT statement.
SQL_Str = “INSERT INTO Addresses “ & “(Name, Street, City, State, Zip) “ & ” VALUES (“ & _
“‘” & Replace$(txtName.Text, “‘”, “””) & “‘, “ & “‘” & _
Replace$(txtStreet.Text, “‘”, “””) & “‘, “ & “‘” & _
Replace$(txtCity.Text, “‘”, “””) & “‘, “ & “‘” & _
Replace$(txtState.Text, “‘”, “””) & “‘, “ & “‘” & _
Replace$(txtZip.Text, “‘”, “””) & “‘” & “)”
‘ Execute the statement.
Conn.Execute SQL_Str, , adCmdText
‘ Close the connection.
Conn.Close

‘ Clear the TextBoxes.

For Each ctl In Controls
If TypeOf ctl Is TextBox Then
ctl.Text = “”
End If
Next ctl

End Sub

Jul 01

How to Import SQL Server data into an MS Access Database

‘Import SQL Server data into an Access database
‘You can quickly import SQL Server data into an Access database using an
‘Execute statement in either ADO or DAO

Private Sub Import_Data()

Dim Conn As ADODB.Connection
Dim SQL_STR As String
‘Create ADO Connection Object
Set Conn = New ADODB.Connection
Conn.Open “Driver={Microsoft Access Driver (*.mdb)};” & “Dbq=ExportedData.mdb;” & _
“DefaultDir=d:\Temp;” & “Uid=Admin;Pwd=;” SQL_STR = “SELECT * INTO “ & “[table_data] “ & “FROM “ & “[ODBC;Driver=SQL Server; " & _
"SERVER=ServerName;DATABASE=MyTable;" & "UID=MyUser;PWD=MyPassword;].” & _
“[table_data];”
Conn.Execute SQL_STR
Set Conn = Nothing

End Sub

Note : Please change the following :

ServerName = Your SQL Server Name or Address
UID = Your username to access the database
PWD = Your database password

‘you must create a temporary folder in drive c:\, please see sample below
c:\temp\ExportedData.mdb

Jun 29

Remote Connection Setup of SQL Server 2005 Express or SQL Server 2005 Database

Microsoft SQL Server 2005 or Microsoft SQL Server 2005 Express does not allow remote access by default. If you want to remote connect or use another computer in your LAN or network, you must configure on your SQL Server 2005 or the SQL Server 2005 Express.

If you don’t change the settings of the SQL server for remote connection, you may receive an error message.

Sqlcmd: Error: Microsoft SQL Native Client: An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow

Enable remote connections for SQL Server 2005 Express or SQL Server 2005 Developer Edition

To enable remote connections on the instance of SQL Server 2005 Express, please follow the steps below.

1. Locate your Microsoft SQL Server 2005 program on the Start menu on your computer, point to Configuration Tools, and then you will see the menu item SQL Server Surface Area Configuration (please check image below)

2.  Click SQL Server Surface Area Configuration, and then click Surface Area Configuration for Services and Connections.

3. In Database Engine menu, please select the Remote connections and by default the SQL Server 2005 connection type is Local Connections only, please select the Local and Remote connections and select Using both TCP/IP and Name Pipes. Based on my experience, It’s better to use both TCP/IP and Name Pipes as you can see the image below because during the configuration of your ODBC you can use these type TCP/IP or Name Pipes to connect the SQL Server 2005 database.

4. Please Click to confirm the database setting changes.

5. Return to the Surface Area Configuration for Services and Connections page, expand Database Engine, select Service, On the right side, click Stop button, then wait until the MSSQLSERVER (MSSQL$SQLEXPRESS2005) service stops, and then click Start button to restart the MSSQLSERVER (MSSQL$SQLEXPRESS2005) service.

Apr 07

How to Center the form in visual basic

This a simple visual basic 6 code on how to Center the form. First, declare the CenterForm function in module area and place the form on Form_Load Event.

 

 

 

 

 

 

'A very simple code that will Center the form relative to Screen

'You may declare this function to module and call it anywhere in the project.

Private Sub btnCenter_Click()
Call CenterForm(Me)
End Sub
'Declare in the module area

Option Explicit
Public Sub CenterForm(frm As Form)
frm.Move (Screen.Width - frm.Width) / 2, (Screen.Height - frm.Height) / 2
End Sub
Apr 02

Tutorial on how to create a popup menu in visual basic

‘How to create a popup menu
‘A simple VB code which demonstrates how to create a popup menu when user press the right button of the mouse.

‘Create first the menu called mnuPopup in the form.

‘Put the following code in the form1 in MouseDown event

Private Sub Form1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

If Button = 2 Then
' if right mouse button is pressed button = 1 equal to left click in the mouse
PopupMenu mnuPopup ' call the mnuPopup menu
End If

End Sub