I'm fairly new to access but not to vba. I have a form that inserts data to a table with vba
rcrdAdd = "INSERT INTO " & timeTbl & " (ProjectNumber, AssignedTo, Task, taskStart) VALUES ('" & ProjectNumber.Value & "', '" & combobox.value & "', '" & combobox.value & "', '" & datetime.value & "');"
db.Execute rcrdAdd, dbFailOnError
When i run this it does add the record but it also updates the first record in the table with the project number associated with record it just added.
I don't know what or why. Can anyone shed any light here. I have tried to research why and can not find an answer
Your form is bound to a the table's recordsource and it looks like you've created a project number text box that is bound to the field
Then it looks like you're typing in the new project number (ProjectNumber.Value) for the Insert.
That will automatically update the current record when you leave - most likely you're opening the form to the first record in the table
Just create a new TEXTBOX that is UNBOUND - and use that in your INSERT INTO SQL instead of ProjectNumber.Value
Related
I am a data consultant who migrates data I am sent into our system. I have written code that compares the contents of my table against what has been put into oracle, as an extra test. The tables are a little convoluted due to how they relate to each other. But essentially here is my question:
When I look to match two field values and the field doesnt exist I get a parameter pop up box. I want to only run the code if the field exists.
I have tried many things, wrapping an if statement around it but I always get the parameter box, can anyone help there must be an easier way to do this!
If Not DoCmd.OpenQuery("SELECT TOP 1" & MatchValues!FieldName & " FROM " &
MatchValues!ORACLE_TABLE_NAME) Then
MsgBox "moomins"
' strSQL = "INSERT INTO 002_TableValueErrors(ORACLE_TABLE_NAME,TRANSFORM_TABLE_NAME,FIELD_NAME,ORACLE_FIELD_VALUE,TRANSFORM_FIELD_VALUE) "
' strSQL = strSQL & " VALUES (" & MatchValues!ORACLE_TABLE_NAME & "," & MatchValues!TRANSFORM_TABLE_NAME & "," & MatchValues!FieldName & ",'ORACLE: NOT FOUND','ORACLE: NOT FOUND')"
End If
If you deal with Oracle: Have you tried to check if the field exists by querying ALL_TAB_COLUMNS in Oracle?
"Select count(*) from ALL_TAB_COLUMNS where table_name = " & MatchValues!ORACLE_TABLE_NAME & " and COLUMN_NAME = " & MatchValues!FieldName
(Untestet cause currently I have no Oracle Instance available)
I have to publish vacant positions in publication system. But before saving in database it will check the Position title in "Postit" column/field and item number in "itemno" column/field if the text already existing in record in the same row. I dont know if it is correct to put many "WHERE" inside the .Open clause or can I put "AND" to avoid many "WHERE". Can you please help me solve this. Thanks... Some part of the codes is stated below:
Error in this line of code:
.Open("Select * from pop where vacantnotvacant ='" & "vacant" & "' , where Position = '" & ListViewEx1.Items.Item(h).SubItems(2).Text & "'", psipopcon, 2, 3)
Try this in place of your open line
.Open("Select * from pop where vacantnotvacant ='" & "vacant" & "' and Position = '" & ListViewEx1.Items.Item(h).SubItems(2).Text & "'", psipopcon, 2, 3)
I'm stuck on something in MS Access VBA.
I'm trying to add a new record to the table and also adding extra field to a different table.
This is part of the vba code I'm using.
'Save the match to the table
DoCmd.GoToRecord , , acNewRec
'Step from 1 to the ResultValue
For LCounter = 1 To ResultHomeTeam.Value
Select Case LCounter
Case 1
dbs.Execute " INSERT INTO tblMatchPlayer " _
& "(MatchID, PlayerID, SubstituteID, PositionID, Surname, ScoreTime, RedCards, YellowCards, Substitude, Penalty, OwnGoal, Assist) VALUES " _
& "(" & Me.MatchID & ", '', '', '', '" & Me.cmScoreName1 & "', " & Me.tbScoreTime1 & ", '', '', '', " & Me.cbPenalty1 & ", " & Me.cbOwnGoal1 & ", '" & Me.cmAssist1 & "');"
However this isn't working, if I only use the dbs.Execute it is writing the values to the correct table but then it isn't writing the Match details to the Match Table
I've got it like this
1st part of the form is match details
Date
Opponent
ScoreHomeTeam
ScoreAwayTEam
That should be saved by acNewRec
And the second part of the form are player details who scored
PlayerName
ScoreTime
OwnGoal
Assist
That has to be saved by dbs.execute
No I got two buttons one, to save the match details on it's own and one to save the player details.
But I would like to have one button to save all of it, but when I try to combine acNewRec and dbs.Execute it isn't working.
The problem is I have to save the match details first before I can save the player details cause player details has to have MatchID.
Can anybody help me?
With kind regards,
Patrick
The problem part was the first save of the form data.
To save the current record (and stay on it), use DoCmd.RunCommand acCmdSaveRecord, not DoCmd.GoToRecord , , acNewRec.
The latter does save, but goes to a new, empty record on the form, so you can't use the previously entered values.
So right now I have a listbox that gets data from an access database and prints it out like below:
When the user clicks on "Just Lose It" for example, and then clicks "Remove Song" button, how can I delete that entry from the database and then refresh the list box with the updated table.
I was thinking of creating an SQL DELETE Statement and then use a ADODB.Recordset to run that statement but I'm not sure how to tell the user clicked that entry.
Any ideas?
Use something like the following... add unique key if in hidden column
sqlStr = "DELETE FROM music WHERE [Title] = '" & music_lb.column(0) & "' AND [Artist] = '" & music_lb.column(1) & "' AND [Album] = '" & music_lb.column(2) & "';"
A friend and I have been trying for hours with little progress to a get a piece of code right for an invoicing system we're designing as a project.
We are trying to update the field InvoiceNo to a value (worked out earlier in the VisualBasic code), where the CustomerNo is the is a specific value and the FinishDate is between two dates. At first I was trying to use TO_DATE but then we realized that wasn't the same in the SQL that Access uses (after much searching).
This has been the simple statement I've been using to just test and try to get something working to then translate into VisualBasic and put in our variables. It's a little easier to read so I thought I'd provide it.
UPDATE tblJob SET tblJob.InvoiceNo = '8' WHERE tblJob.CustomerNo = '1' AND (tblJob.FinishDate BETWEEN cdate(format('08/09/2013', '##/##/####')) AND cdate(format('03/10/2013', '##/##/####')));
I have a feeling after looking at a few examples that our date is meant to be without an forward slashes. So I tried that and it wasn't working either.
Here's the VisualBasic code that has come out of all of this, it's exactly the same but using some variables rather than our set values that I've been using for testing.
DoCmd.RunSQL ("UPDATE tblJob SET tblJob.InvoiceNo = '" & newInvoiceNo & "' WHERE tblJob.CustomerNo = '" & VbCustNo & "' AND (tblJob.FinishDate BETWEEN cdate(format('" & Forms![frmMainMenu][txtFirstDate] & "', '##/##/####')) AND cdate(format('" & Forms![frmMainmenu][txtEndDate] & "', '##/##/####')));")
We had a look at: Convert a string to a date in Access and it helped us realize that it was cdate(format()) rather than TO_DATE as it is in Oracle. But we just can't seem to get it to run properly, any help would be much appreciated.
If you will be running the query from within an Access application session, you can let the db engine use the Access expression service to grab the values from the text boxes on your form.
Dim db As DAO.Database
Dim strUpdate As String
strUpdate = "UPDATE tblJob" & vbCrLf & _
"SET InvoiceNo = '" & newInvoiceNo & "'" & vbCrLf & _
"WHERE CustomerNo = '" & VbCustNo & "'" & vbCrLf & _
"AND FinishDate BETWEEN Forms!frmMainMenu!txtFirstDate AND Forms!frmMainmenu!txtEndDate;"
Debug.Print strUpdate
Set db = CurrentDb
db.Execute strUpdate, dbFailOnError
Set db = Nothing
However, if you prefer to build the literal date values from those text boxes into your UPDATE statement, you can use Format().
"AND FinishDate BETWEEN " & _
Format(Forms!frmMainmenu!txtFirstDate, "\#yyyy-m-d\#") & _
" AND " & Format(Forms!frmMainmenu!txtEndDate, "\#yyyy-m-d\#") & ";"
Either way, using a string variable to hold your UPDATE statement gives you an opportunity to examine the completed statement you're asking the db engine to execute.
You can view the output from Debug.Print in the Immediate window (go there with Ctl+g). For troubleshooting, you can copy the statement text from there and then paste it into SQL View of a new Access query.