Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

insert records to access DB from forms 2

Status
Not open for further replies.

koolkebab

Technical User
Jul 17, 2008
30
US
Hi guys,
I am a newbie programmer in VBA and facing a lot of difficulties with it. I am creating a simple project which allows the user to add, update, delete and find records from a set of tables.
I am working on the Insert data module. I essentially have to populate 2 tables from the inputs given by the user on Access forms.

Problems:
1) One of the inputs in the form is of attachments (Access 2007). I dont know how to read and store these in the table. I also have text boxes, drop downs and memo type variables in the table. How should I read the information form the form to store it in a table. This is the code I am using but does not work.

Private Sub AddRecord_Click()

DoCmd.RunSQL "INSERT INTO tblManagerInfo ([FirstName],[LastName],[StAddress],[State]," _
& "[Country],,[Phone],[Fax],[Website],[AssetsUnderManagement],[Notes])" _
& "VALUES (AddFirstName,AddLastName,AddStAddress,AddState,AddCountry,AddEmail," _
& "AddPhone,AddFax,AddWebsite,AddAUM,AddNotes)"

DoCmd.RunSQL "INSERT INTO tblInvestment ([FirstName],[LastName],[FundName],[StrategyMainCategory]," _
& "[StrategySubCategory],[MinCommitment],[TargetReturn],[Leverage],[Fees],[Geography]," _
& "[Likelyhood],[Status],[Source],[OtherLPs])" _
& "Value(AddFirstName, AddLastName, AddFundName, AddStrategyMainCategory, " _
& "AddStrategySubCategory, AddMinCommitment, AddTargetReturn, AddLeverage, AddFees, " _
& "AddGeography, AddLikelyhood, AddStatus, AddSource, AddOtherLPs)"

End Sub

2) I also have a criteria that if the certain fields (PKs) are not populated then the Inserts operation should not go ahead.

Code:
If AddFirstName = Null Or AddLastName = Null Or AddFundName = Null Then
X = MsgBox("Please Enter the Fields with the * mark", vbCritical, "Incomplete form")
End If

Even before this runs the DoCmd.RunSql command runs, even if I out the DoCmd.RunSql in the Else part of the If statement. I just cannot figure this out. Its kinda frustrating. Please help.

Thanks,
Koolkebab
 
But how do I generalize this. This will be good only when the user puts in the Firstname. Anything else would give him unwanted records.
 
You have lost me. What do you want to happen? With Or all records will be returned unless the user fills in every control and then an odd selection of records will be returned. With And the records returned will be gradually narrowed as each control is filled in. If only one control is completed only records that match that one control will be returned, if two controls are completed, records that match both the controls will be returned:

[tt]Jane Allen
Jane Allen
Jane Doe
Jane Whatever
Mary Whatever

t.FirstName Like '*Jane*' AND t.Surname like '**'
4 records returned

t.FirstName Like '*Jane*' AND t.Surname like 'Whatever'
1 record returned

t.FirstName Like '*Jane*' Or t.Surname like '**'
5 record returned

t.FirstName Like '*Jane*' Or t.Surname like 'Whatever'
5 record returned[/tt]
 
Please check out the screen shot of the screen that I have. You are exactly on the right track. So are you suggesting that I have "**" as the default. So if the user puts in any value instead of default the default would allow you to select the desired records.
 
Oops! sorry about that. I thought of uploading the screenshot from my hard drive.
 
I understood what you are saying and I just now tried it. But it doesnt seem to work :(.

Just to be sure I went ahead and added some more constraints:

SELECT tblInvestmentManager.*
FROM tblInvestmentManager
WHERE (((tblInvestmentManager.ManagerName) Like ("*" & [Forms]![frmFindRecord]![FindManagerName] & "*") Or
(tblInvestmentManager.ManagerName) Like ([Forms]![frmFindRecord]![FindManagerName] & "*") Or
(tblInvestmentManager.ManagerName) Like ("*" & [Forms]![frmFindRecord]![FindManagerName]) Or
(tblInvestmentManager.ManagerName) Like [Forms]![frmFindRecord]![FindManagerName])) AND

(((tblInvestmentManager.FundName) Like ("*" & [Forms]![frmFindRecord]![FindFundName] & "*") Or
(tblInvestmentManager.FundName) Like ([Forms]![frmFindRecord]![FindFundName] & "*") Or
(tblInvestmentManager.FundName) Like ("*" & [Forms]![frmFindRecord]![FindFundName]) Or
(tblInvestmentManager.FundName) Like [Forms]![frmFindRecord]![FindFundName]) AND

((tblInvestmentManager.FirstName) Like ("*" & [Forms]![frmFindRecord]![FindFirstName] & "*") Or
(tblInvestmentManager.FirstName) Like ([Forms]![frmFindRecord]![FindFirstName] & "*") Or
(tblInvestmentManager.FirstName) Like ("*" & [Forms]![frmFindRecord]![FindFirstName]) Or
(tblInvestmentManager.FirstName) Like [Forms]![frmFindRecord]![FindFirstName]) AND

((tblInvestmentManager.LastName) Like ("*" & [Forms]![frmFindRecord]![FindLastName] & "*") Or
(tblInvestmentManager.LastName) Like ([Forms]![frmFindRecord]![FindLastName] & "*") Or
(tblInvestmentManager.LastName) Like ("*" & [Forms]![frmFindRecord]![FindLastName]) Or
(tblInvestmentManager.LastName) Like [Forms]![frmFindRecord]![FindLastName]) AND

((tblInvestmentManager.OfficeLocation2) Like ("*" & [Forms]![frmFindRecord]![FindOfficeLocation] & "*") Or
(tblInvestmentManager.OfficeLocation2) Like ([Forms]![frmFindRecord]![FindOfficeLocation] & "*") Or
(tblInvestmentManager.OfficeLocation2) Like ("*" & [Forms]![frmFindRecord]![FindOfficeLocation]) Or
(tblInvestmentManager.OfficeLocation2) Like [Forms]![frmFindRecord]![FindOfficeLocation]) AND

((tblInvestmentManager.StrategyMainCategory) Like ("*" & [Forms]![frmFindRecord]![FindMainStrategy] & "*") Or
(tblInvestmentManager.StrategyMainCategory) Like ([Forms]![frmFindRecord]![FindMainStrategy] & "*") Or
(tblInvestmentManager.StrategyMainCategory) Like ("*" & [Forms]![frmFindRecord]![FindMainStrategy]) Or
(tblInvestmentManager.StrategyMainCategory) Like [Forms]![frmFindRecord]![FindMainStrategy]) AND

((tblInvestmentManager.StrategySubCategory) Like ("*" & [Forms]![frmFindRecord]![FindSubStrategy] & "*") Or
(tblInvestmentManager.StrategySubCategory) Like ([Forms]![frmFindRecord]![FindSubStrategy] & "*") Or
(tblInvestmentManager.StrategySubCategory) Like ("*" & [Forms]![frmFindRecord]![FindSubStrategy]) Or
(tblInvestmentManager.StrategySubCategory) Like [Forms]![frmFindRecord]![FindSubStrategy]) AND

((tblInvestmentManager.Geography) Like ("*" & [Forms]![frmFindRecord]![FindGeography] & "*") Or
(tblInvestmentManager.Geography) Like ([Forms]![frmFindRecord]![FindGeography] & "*") Or
(tblInvestmentManager.Geography) Like ("*" & [Forms]![frmFindRecord]![FindGeography]) Or
(tblInvestmentManager.Geography) Like [Forms]![frmFindRecord]![FindGeography]) AND

((tblInvestmentManager.Likelyhood) Like ("*" & [Forms]![frmFindRecord]![FindLikelyhood] & "*") Or
(tblInvestmentManager.Likelyhood) Like ([Forms]![frmFindRecord]![FindLikelyhood] & "*") Or
(tblInvestmentManager.Likelyhood) Like ("*" & [Forms]![frmFindRecord]![FindLikelyhood]) Or
(tblInvestmentManager.Likelyhood) Like [Forms]![frmFindRecord]![FindLikelyhood]) AND

((tblInvestmentManager.Status) Like ("*" & [Forms]![frmFindRecord]![FindStatus] & "*") Or
(tblInvestmentManager.Status) Like ([Forms]![frmFindRecord]![FindStatus] & "*") Or
(tblInvestmentManager.Status) Like ("*" & [Forms]![frmFindRecord]![FindStatus]) Or
(tblInvestmentManager.Status) Like [Forms]![frmFindRecord]![FindStatus]));

Clicking on the Find button still doesnt give me the desired results. Do we have to run any command after running every query or something to clear the buffer. I dont know if I am making sense.
 
That is a lot of unnecessary typing. Please say what you wish to achieve. You still have not answered this question. Please show some lines of sample data with what you want returned. For example:

Example 1:
[tt]------------------------------
Forename Surname Status
------------------------------
Mary Able 1
Joe Bloggs 2
Fred Candy 2
Mary Able 3
Joe Bloggs 4
Fred Candy 5
------------------------------

WHERE Forename Like 'Mary' Or Status = 2
Result:
------------------------------
Mary Able 1
Joe Bloggs 2
Fred Candy 2
Mary Able 3
------------------------------

WHERE Forename Like 'Mary' Or Status Like '**'
Result:
------------------------------
Mary Able 1
Joe Bloggs 2
Fred Candy 2
Mary Able 3
Joe Bloggs 4
Fred Candy 5
------------------------------[/tt]

Example 2:
[tt]------------------------------
Forename Surname Status
------------------------------
Mary Able 1
Joe Bloggs 2
Fred Candy 2
Mary Able 3
Joe Bloggs 4
Fred Candy 5

WHERE Forename Like 'Mary' Or Status = 2
Result:
------------------------------
Mary Able 1
------------------------------

WHERE Forename Like 'Mary' AND Status Like '**'
Result:
------------------------------
Mary Able 1
Mary Able 3
------------------------------
[/tt]
 
I am really sorry for ambiguity. I want a find function which can find information from a database.
eg:
Database ->
------------------------------
Forename Surname Status
------------------------------
Mary Able 1
Joe Bloggs 2
Fred Candy 2
Mary Able 3
Joe Bloggs 4
Fred Candy 5
Mary Bloggs 6
------------------------------

On the form ->
3 txt boxes with Forename, Surname and Status and 1 button, Find
1) When I type Mary in Forename box and press find button-
results in the subform to be displayed:

------------------------------
Forename Surname Status
------------------------------
Mary Able 1
Mary Able 3
Mary Bloggs 6
------------------------------

2) When I type Bloggs in Surname box and press find button-
results in the subform to be displayed:

------------------------------
Forename Surname Status
------------------------------
Joe Bloggs 2
Joe Bloggs 4
Mary Bloggs 6
------------------------------

3) When I type Mary in Forename box and Bloggs in Surname box and press find button-
results in the subform to be displayed:

------------------------------
Forename Surname Status
------------------------------
Mary Bloggs 6
------------------------------

I hope I am making sense. Thanks!
 
There is one more problem I cannot figure out. I want to verify the username and password on the main screen.
Problems:
1) How do I make the whole Access project to open to the username and passwrod screen.
2) I am using the code below to open the main form if the passwrod is correct ->

Private Sub SignIn_Click()
Dim cnn1 As ADODB.Connection
Set cnn1 = CurrentProject.Connection
Dim RecordSecurity As New ADODB.Recordset
RecordSecurity.ActiveConnection = cnn1
RecordSecurity.Open "Select * from tblSecurity"

If Me!username.Value = RecordSecurity!username And Me!password.Value = RecordSecurity!password Then
X = MsgBox("Correct password", vbOKOnly)
DoCmd.OpenForm frmMain
Else
X = MsgBox("Incorrect password", vbOKOnly)

End If
End Sub

Also here tblSecurity has just one row of information: the username and the password.
According to me, this is correct, but it still gives me an error - '..requires Form Name argument'. How do I get this problem solved?
 
This thread is long enough. In Tek-Tips, it is usual to have one question per thread, so it is time to start a new thread.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top