473,396 Members | 2,021 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

To Add record in Database

Hi,

I have created a form in Visual basic 6.0 for adding/updating the change request made by various users.

Well i am able to update the record through my code in access database.

Can anyone tell me what code should i use to Add record in the database, i also want the new record which will be added in the access database is having some unique no like CRC-CC-date/month/year-incremental number?

I mean as soon anyone will hit the add record form should become compltly blank and with a unique no as per above format.And same has to be updated in the database.

Below is my code, where i have reached

Private Sub Form_Load()
strMyDB = App.Path & "\" & "MyDB.mdb"
txtSearch.Text = DefaultSearchText
End Sub

Private Sub cmdSearch_Click()
'Search for a client

Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strSQL As String
Dim arWords, iWord As Long
Dim xItem As ListItem
Dim curField As Field
Dim i As Integer
lvClients.ListItems.Clear
lvClients.ColumnHeaders.Clear

'Open DB connection
'for other DB connection strings go to http://www.thescripts.com/forum/thread572278.html
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strMyDB & ";"

'Put the words searched for into an array and fix any apostrophies
arWords = Split(FixApostrophies(txtSearch.Text), " ")

'Start query
strSQL = "SELECT * FROM clients WHERE"

'Build filter requiring all words entered
' to be in username, name_first, or name_last field
For iWord = 0 To UBound(arWords)
If iWord > 0 Then strSQL = strSQL & " AND"
strSQL = strSQL & " ("
strSQL = strSQL & "username like '%" & arWords(iWord) & "%'"
strSQL = strSQL & " OR"
strSQL = strSQL & " name_first like '%" & arWords(iWord) & "%'"
strSQL = strSQL & ")"
Next 'iWord

'Query the database
rs.Open strSQL, cn

'Create column headers for listview based on field names
If Not rs.EOF Then
For Each curField In rs.Fields
lvClients.ColumnHeaders.Add , , curField.name
Next 'curField
End If

'Populate listview with recordset
While Not rs.EOF
'Debug.Print rs("username") & vbTab & rs("phone")
Set xItem = lvClients.ListItems.Add(, , rs.Fields(0).Value)
For i = 1 To (rs.Fields.Count - 1)
xItem.ListSubItems.Add , , rs.Fields(i).Value
Next
'Move to next record in recordset
rs.MoveNext
Wend

'close recordset/connection
rs.Close
cn.Close
'remove references
Set rs = Nothing
Set cn = Nothing
End Sub

Private Sub cmdUpdate_Click()
'Update client's record
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strSQL As String

'Open DB connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strMyDB & ";"

'create SQL statement to select client from a unique record id
strSQL = "SELECT * FROM clients" & _
" WHERE client_id=" & FixApostrophies(txtClient_Id.Text)

rs.Open strSQL, cn, adOpenForwardOnly, adLockOptimistic

'See if there's a record found
If rs.EOF Then
'record not found
MsgBox "That record no longer exists"
Else
'record found, update record
rs("username") = txtUsername.Text
rs("name_last") = txtName_Last.Text
rs("name_first") = txtName_First.Text
rs("phone") = txtPhone.Text
'update the recordset
rs.Update
End If
'close recordset/connection
rs.Close
cn.Close
'remove references
Set rs = Nothing
Set cn = Nothing
End Sub

Private Sub lvClients_ItemClick(ByVal Item As MSComctlLib.ListItem)
Dim header As ColumnHeader
Dim ret

'Loop through column headers, and populate textboxes with same name (but have 'txt' prefix)
For Each header In lvClients.ColumnHeaders
On Error Resume Next
ret = Me.Controls("txt" & header.Text)
If Err.Number <> 0 Then
MsgBox Err.Description
Err.Clear
On Error GoTo 0
GoTo NextHeader:
End If

If header.Index > 1 Then
Me.Controls("txt" & header.Text).Text = Item.ListSubItems(header.Index - 1).Text
Else
Me.Controls("txt" & header.Text).Text = Item.Text
End If
NextHeader:
Next 'header
End Sub

Function FixApostrophies(ByVal sInput As String) As String
'Use for text that will be included as part of a query
If InStr(1, sInput, "'") Then
'Fix apostrophies
FixApostrophies = Replace(sInput, "'", "''")
Else
FixApostrophies = sInput
End If
End Function

Function RandomInt(ByVal HighVal As Long, Optional ByVal LowVal As Long = 0) As Long
Randomize
RandomInt = CLng((HighVal * Rnd) + LowVal)
End Function

Private Sub Scriptlet1_onscriptletevent(ByVal name As String, ByVal eventData As Variant)

End Sub

Private Sub txtClient_Id_Change()
cmdUpdate.Enabled = True
End Sub

'########### Everthing below is unnecessary code ##########
Private Sub txtSearch_GotFocus()
cmdSearch.Default = True
If txtSearch.Text <> "" And txtSearch.Text = DefaultSearchText Then
txtSearch.Text = Empty
Else
txtSearch.SelStart = 0
txtSearch.SelLength = Len(txtSearch.Text)
End If
End Sub


Private Sub txtSearch_LostFocus()
cmdSearch.Default = False
If txtSearch.Text = "" Then txtSearch.Text = DefaultSearchText
End Sub



Kindly help me. Thanx in advance.
Sep 17 '07 #1
3 2252
hariharanmca
1,977 1GB
You never point what you had tried for insert query.

let cn is your connection

Expand|Select|Wrap|Line Numbers
  1. Dim strSql as string
  2.  
  3. strSql="Insert into <Table Name> (field1,field2,field3) values("  & value1 & ", "  & value1 & ", "  & value1 & ")".
  4. cn.Execute strsql
Sep 17 '07 #2
actually i tried few things but didn't work.

I have the tried the above code which is given by you, but it is also not working i am geting error of Sub/function not defined,

Private Sub CmdAdd_Click()
Dim strSql As String
Dim cn As New ADODB.Connection
Dim n As String
Where n = 0
strSql = "Insert into <clients> (txtUsername),values(CRC-CC-today()-n)"
strSql = "Insert into <clients> (txtUsername),values(CRC-CC-today()-n+1)"
cn.Execute strSql
End Sub

Above is the code. Plz go thru it if its correct or changes required.
Sep 17 '07 #3
hariharanmca
1,977 1GB
actually i tried few things but didn't work.

I have the tried the above code which is given by you, but it is also not working i am geting error of Sub/function not defined,

Private Sub CmdAdd_Click()
Dim strSql As String
Dim cn As New ADODB.Connection
Dim n As String
Where n = 0
strSql = "Insert into <clients> (txtUsername),values(CRC-CC-today()-n)"
strSql = "Insert into <clients> (txtUsername),values(CRC-CC-today()-n+1)"
cn.Execute strSql
End Sub

Above is the code. Plz go thru it if its correct or changes required.

I think CRC-CC-today() is an VB method and also n.
so the code will be..

Expand|Select|Wrap|Line Numbers
  1. strSql = "Insert into clients (txtUsername) values(" & CRC-CC-today()-n & ")"
  2. cn.Execute strSql
  3.     strSql = "Insert into clients (txtUsername) values('" & CRC-CC-today()-n+1 & "')"
  4.     cn.Execute strSql

For value type the single quote will not be there
it will appear for String and date time type data.
Sep 17 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: yvan | last post by:
Approximately once a month, a client of ours sends us a bunch of comma-delimited text files which I have to clean up and then import into their MS SQL database. All last week, I was using a Cold...
6
by: DebbieG | last post by:
I have created a database for a client and was told that it was to be a one-user database. Well, you know the next statement ... now they want 3 people to be able to use the database. (FYI, I...
15
by: Steve | last post by:
I have a form with about 25 fields. In the BeforeUpdate event of the form, I have code that sets the default value of each field to its current value. For a new record, I can put the focus in any...
2
by: RC | last post by:
I am getting the following error message. "Write Conflict this record has been changed by another user since you started edting it. If you save the record, you will overwrite the changes...." I...
1
by: Scott269 | last post by:
So I've got an old MS Works database I imported into Access. I needed a primary key so I created a record number field that was just the record number I manually inserted when I entered it in the...
3
by: domcatanzarite | last post by:
How would one create a button that on click advances the form to the next "non recurring record" as opposed to the next record. The field the button needs to que from has groups of duplicate...
6
by: polocar | last post by:
Hi, I'm writing a program in Visual C# 2005 Professional Edition. This program connects to a SQL Server 2005 database called "Generations" (in which there is only one table, called...
10
by: MLH | last post by:
Suppose, in a multi-user environment, you have append query SQL in a VBA procedure that looks like INSERT INTO MyTable... and the next line reads MyVar=DMax("","MyTable... You can never be...
1
by: Sunray | last post by:
I have a form called the sales form and i have 2 sets of listboxes So what happens is. i add items form the bottom set of list boxes which are bound to a data base to the top set of list boxes which...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.