Solved

Strings to MS Access Database

Posted on 2000-02-15
4
148 Views
Last Modified: 2010-05-02
I have 3 strings (sA, sB, sC) with some content.

I would like to know how to insert these strings in a MS Access
Database in the following manner.

Col 1 = sA
Col 2 = sB
Col 3 = sC

Do you know what I mean?

0
Comment
Question by:blue021097
  • 2
4 Comments
 
LVL 6

Expert Comment

by:Marine
Comment Utility
Are these 3 strings different fields ? Or these strings one field ? Which must be inserted into one field ? Here is some sample code. This uses OLEDB provider.

Dim Cn as New ADODB.Connection
Dim sCon as string,sSql as string
sCon = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\my documents\mb.mdb;"
Cn.Open scon
Col1 = sA
Col2 = sB
Col3 = sC
sSql = "Insert Into <tablename> Values ('" & col1 & "','" & col2 & "','" & col3 & "')"
Cn.Execute sSql,adExecuteNoRecords
cn.close
Set cn = nothing
This code will insert records into 3 fields. Explain me if this is not what you looking for.
0
 
LVL 1

Accepted Solution

by:
eab111098 earned 100 total points
Comment Utility
first, col 1,2,3 are not columns persey, they are fields. in excel, they're columns, in access, they're fields. to make the following code work, you need to make a reference to the jet 351 dao control under project->references. with that done... here ya go.

public function addtomydb(byval col1 as string, byval col2 as string, byval col3 as string)

dim DB as database
dim RS as recordset

set db = opendatabase("yourdbpathandname")
set rs = db.openrecordset("nameoftablewhereyouwanttoputthisstuff")
with rs
  .addnew
  .fields("fieldname1") = col1
  .fields("fieldname2") = col2
  .fields("fieldname3") = col3
  .update
end with
rs.close
set rs = nothing
db.close
set db = nothing
end function

now, there are at least 101 other ways of doing this addition, and undoubtedly, you'll receive many more suggestions. i'm not going to tell you this is the best way because the best way is dictated by your application and what you're trying to accomplish. this is just one way.

hope this helps.
later.
ed.
0
 
LVL 6

Expert Comment

by:Marine
Comment Utility
Dude its not necessary to lock the question. If it will be the right answer he can accept your comment. If the question is locked others can't see it since most of them don't browse answered question. I beat your time though :-) i got my answer in first. Blue my example uses ADO method of access his example uses DAO you havent' specified what you were using. My method would be faster for one reason only. It uses SQL to update and it doesn't create a recordset which is a plus.
0
 

Author Comment

by:blue021097
Comment Utility
Marine,

Your answer may very well be right but I have
accepted eab answer as it is the first
I tried out, and it worked. Problem solved.

I hope this is okay?

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now