How Can Fix "run time error 3251" in excel VBA - vba

I am using excel 2016., I used a form to fill data in access, but when click refresh I start getting "run time error 3251" on this one. If can someone help me to find out. please.
I am fetching data from access to excel VBA user form & then doing some updates & deleting as per requirement.
Error
Msg=> "Run-time error '3251': Current Recordset does not support
updating. This may be a limitation of the provider, or of the
selected locktype."
Code
Private Sub CommandButton1_Click()
''''''''Add Validation here '''''''''''''
If IsDate(Me.txtdate1.Value) = False Then
MsgBox "Please enter the correct Transaction_Date", vbCritical
Exit Sub
End If
If Me.txtcampany1.Value = "" Then
MsgBox "Please enter the Campany", vbCritical
Exit Sub
End If
If Me.txttrans1.Value = "" Then
MsgBox "Please enter the Type_Transaction", vbCritical
Exit Sub
End If
If Me.txtdebit.Value <> "" Then
If IsNumeric(Me.txtdebit.Value) = False Then
MsgBox "Please enter the correct Debit", vbCritical
Exit Sub
End If
End If
If Me.txtcredit.Value <> "" Then
If IsNumeric(Me.txtcredit.Value) = False Then
MsgBox "Please enter the correct credit", vbCritical
Exit Sub
End If
End If
If Me.txtbank1.Value = "" Then
MsgBox "Please enter the By_Bank", vbCritical
Exit Sub
End If
If Me.txtStuff1.Value = "" Then
MsgBox "Please enter the Stuff", vbCritical
Exit Sub
End If
If Me.Texremr1.Value = "" Then
MsgBox "Please enter the Comment", vbCritical
Exit Sub
End If
If Me.Textdenu.Value = "" Then
MsgBox "Please enter the Deposits_Number", vbCritical
Exit Sub
End If
If Me.Textattech.Value = "" Then
MsgBox "Please enter the Attchment_File", vbCritical
Exit Sub
End If
If Me.bra1.Value = "" Then
MsgBox "Please enter the Branch", vbCritical
Exit Sub
End If
If Me.depf.Value = "" Then
MsgBox "Please enter the Deposits_For", vbCritical
Exit Sub
End If
'''''''''''''''''''''''''''''''''''''''''
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim qry As String
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.Path & "\Database.accdb"
If Me.txtId.Value <> "" Then
qry = "SELECT * FROM Public_Deposits WHERE ID = " & Me.txtId.Value
Else
qry = "SELECT * FROM Public_Deposits Where ID = 0"
End If
rst.Open qry, cnn, adOpenKeyset, adLockOptimistic
If rst.RecordCount = 0 Then
rst.AddNew
End If
rst.Fields("Transaction_Date").Value = VBA.CDate(Me.txtdate1.Value)
rst.Fields("Campany").Value = Me.txtcampany1.Value
rst.Fields("Type_Transaction").Value = Me.txttrans1.Value
If Me.txtdebit.Value <> "" Then rst.Fields("Debit").Value = Me.txtdebit.Value
If Me.txtcredit <> "" Then rst.Fields("credit").Value = Me.txtcredit
rst.Fields("By_Bank").Value = Me.txtbank1.Value
rst.Fields("Stuff").Value = Me.txtStuff1.Value
rst.Fields("Comment").Value = Me.Texremr1.Value
rst.Fields("Deposits_Number").Value = Me.Textdenu.Value
rst.Fields("Branch").Value = Me.bra1.Value
rst.Fields("Deposits_For").Value = Me.depf.Value
rst.Fields("UpdateTimestamp").Value = VBA.Now
rst.Update
Me.txtdate1.Value = ""
Me.txtcampany1.Value = ""
Me.txttrans1.Value = ""
Me.txtdebit.Value = ""
Me.txtcredit.Value = ""
Me.txtbank1.Value = ""
Me.txtStuff1.Value = ""
Me.Texremr1.Value = ""
Me.Textdenu.Value = ""
Me.bra1.Value = ""
Me.depf.Value = ""
MsgBox "Updated Successfully", vbInformation
Call Me.List_box_Data
End Sub

Related

Pasting hyperlink in textbox user form in VBA

I tried to paste the link of my sharepoint in the destination URL
but I am having this error:
The link I pasted is my Sharepoint link ("https://fujitsu.sharepoint.com/teams/Global-9f53b187/Migration/Forms/AllItems.aspx")
This is the code for the 'Migration Start' button
Private Sub btnMigrate_Click()
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
Dim answer As Integer
FromPath = txtPath.Value
ToPath = txtURL.Value
txtPath.BackColor = vbWhite
txtURL.BackColor = vbWhite
If txtPath.Value = "" Then
MsgBox "Enter the migration source path", vbOKOnly + vbInformation, "WARNING!"
txtPath.BackColor = vbRed
ElseIf txtURL.Value = "" Then
MsgBox "Enter the destination URL", vbOKOnly + vbInformation, "WARNING!"
txtURL.BackColor = vbRed
ElseIf Not IsEmpty(txtPath.Value) And Not IsEmpty(txtURL.Value) Then
Me.Hide
Set FSO = CreateObject("scripting.filesystemobject")
FSO.CopyFolder Source:=FromPath, Destination:=ToPath
txtPath.Value = ""
txtPath.BackColor = vbWhite
txtURL.Value = ""
txtURL.BackColor = vbWhite
MsgBox "The migration was successful!", vbOKOnly + vbInformation, "SUCCESSFUL!"
answer = MsgBox("Would you like to migrate again?", vbQuestion + vbYesNo + vbDefaultButton2, "QUESTION")
If answer = vbYes Then
txtPath.Value = ""
txtPath.BackColor = vbWhite
txtURL.Value = ""
txtURL.BackColor = vbWhite
Migration.Show
Else
Me.Hide
End If
End If
End Sub

How to take values from an open Access continuous sub form, and "paste" them into another form?

Okay. Let me try to explain what is happening here.
User will select record in form 1 and click button.
That will open form 2 to a detail form of that record.
User will then select one or multiple codes from Form 2 and click order button.
Form 3 will open with the info from Form 2, but I am having trouble getting the codes to fill in on Form 3. This is where I need help.
Existing code as follows:
**Form 1 CODE**
Option Compare Database
Option Explicit
Private Sub RequeryForm()
Dim SQL As String, WhereStr As String
WhereStr = ""
If Search <> "" Then
If WhereStr <> "" Then WhereStr = WhereStr & " AND "
WhereStr = "LocationID Like ""*" & AccountSearch & "*"""
End If
If NameSearch <> "" Then
If WhereStr <> "" Then WhereStr = WhereStr & " AND "
WhereStr = "FirstNameLastName Like ""*" & NameSearch & "*"""
End If
If CodeSearch <> "" Then
If WhereStr <> "" Then WhereStr = WhereStr & " AND "
WhereStr = "Code Like ""*" & CodeSearch & "*"""
End If
SQL = "Select * From AMSQuery"
If WhereStr <> "" Then
SQL = SQL & " Where " & WhereStr
End If
Me.RecordSource = SQL
End Sub
Private Sub ClearSearchBtn_Click()
SetDefaults
RequeryForm
End Sub
Private Sub OpenDetailbtn_Click()
DoCmd.OpenForm "Form2", , , "LocationID=" & Me.LocationID
End Sub
Private Sub SearchBtn_Click()
RequeryForm
End Sub
Private Sub SetDefaults()
AccountSearch = Null
NameSearch = Null
CodeSearch = Null
End Sub
**Code For Form2**
Private Sub ExitBTN_Click()
DoCmd.Close acForm, "Form2"
End Sub
Private Sub OrderILbtn_Click()
DoCmd.OpenForm "RequestForm", acNormal, , , acFormAdd
End Sub
**Form 3 Code**
Option Compare Database
Option Explicit
'Private Sub IncNumber_BeforeUpdate(Cancel As Integer)
'If Not (Me!IncNumber = "IncNumber" Or (Me!IncNumber <> 11) Or IsNull(Me!IncNumber)) Then
'MsgBox "The Incident Number entered is less than 11 characters."
'Cancel = True
'End If
'End Sub
Private Sub CloseFormBtn_Click()
DoCmd.Close acForm, "Form3", acSaveYes
DoCmd.SelectObject acForm, "Form1"
End Sub
Private Sub Form_Load()
Forms!RequestForm!Account = Forms!Form2!LocationID
End Sub
Private Sub SaveBtn_Click()
If IsNull([Account]) Then
MsgBox "You forgot to add a Y account.", vbOKOnly, "Missing Y account Warning!"
Else
DoCmd.RunCommand acCmdSaveRecord
DoCmd.GoToRecord , , acNewRec
End If
'ILRequestID = "IL" & Right(Year([DateAndTimeRequested]), 2) & Format(Month([DateAndTimeRequested]), "00") & Format(Day([DateAndTimeRequested]), "00") & [EntryID]
End Sub

Import Excels with table checking - access 2016

I have googled everywhere but I am unable to find out to do it without rewriting all the code, is there anyway to have this code check whether the file name matches table names and if it does then clear that table and re import or if not then create a new table?
Option Compare Database
Option Explicit
Private Sub btnBrowse_Click()
Dim diag As Office.FileDialog
Dim item As Variant
Set diag = Application.FileDialog(msoFileDialogFilePicker)
diag.AllowMultiSelect = False
diag.Title = "Please select an Excel Spreadsheet"
diag.Filters.Clear
diag.Filters.Add "Excel Spreadsheets", "*.xls, *.xlsx, *.xlsm"
If diag.Show Then
For Each item In diag.SelectedItems
Me.txtFileName = item
Next
End If
End Sub
Private Sub btnImportSpreadsheet_Click()
Dim FSO As New FileSystemObject
If FSO.FileExists(Nz(Me.txtFileName, "")) Then
ImportExcelSpreadsheet Me.txtFileName, FSO.GetFileName(Me.txtFileName)
ElseIf Nz(Me.txtFileName, "") = "" Then
MsgBox "Please select a file!", vbExclamation
Else
MsgBox "File not found!", vbExclamation
End If
End Sub
Public Sub ImportExcelSpreadsheet(Filename As String, TableName As String)
On Error Resume Next
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, TableName, Filename, True
If Err.Number = 3125 Then
If vbOK = MsgBox(Err.Description & vbNewLine & vbNewLine & "Skip column header and continue?", vbExclamation + vbOKCancel, "Error with Excel Column header") Then
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, TableName, Filename, False
MsgBox "Done", vbInformation
End If
Exit Sub
ElseIf Err.Number <> 0 Then
MsgBox Err.Number & ":" & Err.Description, vbCritical
Exit Sub
End If
MsgBox "Upload Complete", vbInformation
End Sub
Thank for any help
You'll have to rewrite some. Without looping through Tables collection and testing against each name, every method seems to involve handling an error. Here is one:
Function TableExists(strTableName As String) As Boolean
On Error Resume Next
TableExists = IsObject(CurrentDb.TableDefs(strTableName))
End Function
Call the function:
If TableExists("YourTableName") = True Then
More examples in How to check if a table exists in MS Access for vb macros

Error 3421 Data Type Connection Error Multy Column Combobox

I have a scenario where in I have to save "STATUS" code into table from a "COMBO BOX". This Combo Box shows "Status ID" and "Status Description" together. But while saving I need to save only "Status ID"
Following is the code for the complete form functionality. Error is occurring on click of "SAVE" button. On line where I am assigning the value from combo to "Recordset Column" on line # 77 or 90.
' rs![status_ID] = Me.cboStatus.Column(1)
Option Compare Database
Option Explicit
Dim db As Database
Dim rs, rs2, rs3 As Recordset
Dim SQL, SQL1, SQL2 As String
Dim intChk As Integer
Private Sub btnFirst_Click()
If Not rs.BOF Then
rs.MoveFirst
Set_Data
End If
If rs.EOF Then
rs.MovePrevious
End If
End Sub
Private Sub btnLast_Click()
If Not rs.EOF Then
rs.MoveLast
Set_Data
End If
If rs.EOF Then
rs.MovePrevious
End If
End Sub
Private Sub btnNew_Click()
SQL2 = "select Max(job_ID) as JID from tbl_mst_JobOrder"
Set rs3 = CurrentDb.OpenRecordset(SQL2, dbOpenDynaset, dbSeeChanges)
If Not rs3.EOF And Not rs3.BOF Then
Me.txtJobID = rs3!JID + 1
End If
Set rs3 = Nothing
TxtSetEmpty
End Sub
Private Sub btnNext_Click()
If Not rs.EOF Then
rs.MoveNext
Set_Data
End If
If rs.EOF Then
rs.MovePrevious
End If
End Sub
Private Sub btnPrevious_Click()
If Not rs.BOF Then
rs.MovePrevious
Set_Data
End If
If rs.BOF Then
rs.MoveNext
End If
End Sub
Private Sub btnSave_Click()
Dim SQL As String
IfEmpty
Dim sqlShift As String
If intChk = 1 Then
intChk = 0
Exit Sub
Else
SQL = "select job_ID from qryJobDetails " _
& "where job_ID = " & Me.txtJobID
Set rs2 = CurrentDb.OpenRecordset(SQL)
If Not rs2.EOF Then
Dim CHK As String
Me.lblChk.Caption = rs2![job_ID]
End If
Set rs2 = Nothing
If Me.txtJobID.Value = Me.lblChk.Caption Then
Dim msgUpd, msgNew, strCobSt As String
strCobSt = Me.cboStatus.Column(1)
msgUpd = "Do you want to update Location ID " & Me.lblChk.Caption
If MsgBox(msgUpd, vbYesNo, "Location Update") = vbYes Then
rs.Edit
rs![job_Date] = Me.dtpJDate.Value
rs![job_Desc] = Me.txtJobDesc
rs![loc_ID] = Me.txtLocID
rs![status_ID] = Me.cboStatus.Column(1)
rs![Comments] = Me.txtComment
rs.Update
RefreshListBox
End If
Else
msgNew = "Do you want to add New Location"
If MsgBox(msgNew, vbYesNo, "Add New Location") = vbYes Then
rs.AddNew
rs![job_ID] = Me.txtJobID
rs![job_Date] = Me.dtpJDate.Value
rs![job_Desc] = Me.txtJobDesc
rs![loc_ID] = Me.txtLocID
rs![status_ID] = Me.cboStatus.Column(1)
rs![Comments] = Me.txtComment
rs.Update
RefreshListBox
End If
End If
End If
End Sub
Private Sub Form_Load()
Set db = CurrentDb
SQL = "Select status_ID, status_Desc from tbl_mst_Status order by status_ID"
Set rs2 = db.OpenRecordset(SQL)
Do Until rs2.EOF
Me.cboStatus.AddItem rs2![status_ID] & "|" & rs2![status_Desc]
rs2.MoveNext
Loop
Set rs2 = Nothing
Set rs = db.OpenRecordset("qryJobDetails", dbOpenDynaset, dbSeeChanges)
RefreshListBox
Set_Data
End Sub
Private Sub Set_Data()
If Not rs.BOF And Not rs.EOF Then
Me.txtJobID = rs![job_ID]
Me.dtpJDate = rs![job_Date]
Me.txtJobDesc = rs![job_Desc]
Me.txtLocID = rs![loc_ID]
Me.txtLocDec = rs![location_desc]
Me.cboStatus = rs![status_ID] & "|" & rs![status_Desc]
Me.txtComment = rs![Comments]
End If
End Sub
Private Sub RefreshListBox()
Me.lstJobOrd.RowSource = ""
Me.lstJobOrd.AddItem "Job Order" & ";" & "Job Date" & ";" & "Job Description" & ";" _
& "Loc Description" & ";" & "Loc ID" & ";" & "Sta ID" & ";" _
& "Sta Desc" & ";" & "Comments"
rs.MoveFirst
Do Until rs.EOF
Me.lstJobOrd.AddItem rs![job_ID] & ";" & rs![job_Date] & ";" & rs![job_Desc] & ";" _
& rs![location_desc] & ";" & rs![loc_ID] & ";" & rs![status_ID] & ";" _
& rs![status_Desc] & ";" & rs![Comments]
rs.MoveNext
Loop
rs.MoveFirst
End Sub
Private Sub TxtSetEmpty()
Me.txtJobDesc = ""
Me.dtpJDate = Now()
Me.txtLocDec = ""
Me.cboStatus = ""
Me.txtComment = ""
Me.txtLocID = ""
End Sub
Private Sub lstJobOrd_Click()
With Me.lstJobOrd
Me.txtJobID.Value = .Column(0)
Me.dtpJDate.Value = .Column(1)
Me.txtJobDesc.Value = .Column(2)
Me.txtLocDec.Value = .Column(3)
Me.txtLocID.Value = .Column(4)
Me.cboStatus.Value = .Column(5)
Me.txtComment.Value = .Column(7)
End With
End Sub
Private Sub IfEmpty()
Dim txtCtr As Control
Dim cboCtr As Control
Dim Str As String
Str = Empty
For Each txtCtr In Me.Controls
If TypeOf txtCtr Is TextBox Then
If IsNullOrEmpty(txtCtr) Then
txtCtr.BackColor = RGB(119, 192, 212)
txtCtr.BorderColor = RGB(157, 187, 97)
Str = Str & txtCtr.Tag & vbNewLine
Else
txtCtr.BackColor = vbWhite
txtCtr.BorderColor = RGB(192, 192, 192)
End If
End If
Next txtCtr
For Each cboCtr In Me.Controls
If TypeOf cboCtr Is ComboBox Then
If IsNullOrEmptyCbo(cboCtr) Then
cboCtr.BackColor = RGB(119, 192, 212)
cboCtr.BorderColor = RGB(157, 187, 97)
Str = Str & cboCtr.Tag & vbNewLine
Else
cboCtr.BackColor = vbWhite
cboCtr.BorderColor = RGB(192, 192, 192)
End If
End If
Next cboCtr
If IsNull(Str) Or Str = "" Then
Exit Sub
Else
MsgBox "Please enter data in the highlited fields. " & vbNewLine & _
String(52, "_") & vbCrLf & Str, vbInformation + vbOKOnly, "Data not Complete"
intChk = 1
Exit Sub
End If
End Sub
Private Sub txtLocDec_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 113 Then
DoCmd.OpenForm "frmLocSer", acNormal, , , acFormAdd, acWindowNormal
End If
End Sub`

Change Text Font within Same Textbox in VBA

I have multiple subs within VBA that all have their output within the same text box (WarningData) in a PPT slide. For example, Sub 1 takes a user selection (a selection they made from a drop down menu within a GUI) and inserts that at the top of the text box. Sub 2 inserts another line of text below that line. Sub 3 inserts additional text below that. I need Sub 1 and 2 to have the same font style, but Sub 3 needs to have a different font.
Here is what Sub 1 and Sub 2 look like:
Private Sub 1() 'Sub 2 is very similar.
Call Dictionary.WindInfo
'Sets the font for the warning information text.
With ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2.TextRange.Font
.Size = 24
.Name = "Calibri"
.Bold = msoTrue
.Shadow.Visible = True
.Glow.Radius = 10
.Glow.Color = RGB(128, 0, 0)
End With
ComboBoxList = Array(CStr(ComboBox3), CStr(ComboBox4))
For Each Ky In ComboBoxList
On Error Resume Next
'If nothing is selected in ComboBox4, do nothing and exit this sub.
If ComboBox4 = "" Then
Exit Sub
ElseIf ComboBox3 = "" Then
ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2.TextRange = _
ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2.TextRange & dict3.Item(Ky)(0)
'Otherwise, if it has a selection, insert selected text.
ElseIf ComboBox3 <> "" Then
ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2.TextRange = _
ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2.TextRange & vbCrLf & vbCrLf & dict3.Item(Ky)(0)
End If
Next
Set dict3 = Nothing
End Sub
The following sub is the one that I need to have a different font style:
Private Sub 3()
Call Dictionary.Call2Action
ComboBoxList = Array(CStr(ComboBox7))
For Each Ky In ComboBoxList
On Error Resume Next
'If nothing is selected in ComboBox7 and TextBox9, do nothing and exit this sub.
If ComboBox7 = "" And TextBox9 = "" Then
Exit Sub
'Otherwise, if either has a selection, insert selected text.
ElseIf ComboBox7 <> "" And TextBox9 = "" Then
ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2.TextRange = _
ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2.TextRange & vbCrLf & vbCrLf & dict7.Item(Ky)(0)
ElseIf ComboBox7 = "" And TextBox9 <> "" Then
ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2.TextRange = _
ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2.TextRange & vbCrLf & vbCrLf & TextBox9
End If
Next
Set dict7 = Nothing
End Sub
Any idea if this is possible?
Thanks!!
I simplified the code using a With statement and added 2 x font lines to show how to set the Font name. Other properties are also available in the Font2 object e.g. .Size, .Bold, .Fill etc.
Private Sub Three()
Call Dictionary.Call2Action
ComboBoxList = Array(CStr(ComboBox7))
For Each Ky In ComboBoxList
On Error Resume Next
With ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2
'If nothing is selected in ComboBox7 and TextBox9, do nothing and exit this sub.
If ComboBox7 = "" And TextBox9 = "" Then
Exit Sub
'Otherwise, if either has a selection, insert selected text.
ElseIf ComboBox7 <> "" And TextBox9 = "" Then
.TextRange = .TextRange & vbCrLf & vbCrLf & dict7.Item(Ky)(0)
.TextRange.Font.Name = "Calibri"
ElseIf ComboBox7 = "" And TextBox9 <> "" Then
.TextRange = .TextRange & vbCrLf & vbCrLf & TextBox9
.TextRange.Font.Name = "Calibri"
End If
End With
Next
Set dict7 = Nothing
End Sub
Using the TextRange.Paragraphs method I was able to accomplish this task:
Private Sub 3()
Call Dictionary.Call2Action
ComboBoxList = Array(CStr(ComboBox7))
For Each Ky In ComboBoxList
On Error Resume Next
With ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2
'If nothing is selected in ComboBox7 and TextBox9, do nothing and exit this sub.
If ComboBox7 = "" And TextBox9 = "" Then
Exit Sub
'Otherwise, if either has a selection, insert selected text.
ElseIf ComboBox7 <> "" And TextBox9 = "" Then
.TextRange = .TextRange & vbCrLf & vbCrLf & dict7.Item(Ky)(0)
.TextRange.Paragraphs(3).Font.Size = 18
.TextRange.Paragraphs(3).Font.Name = "Calibri"
.TextRange.Paragraphs(3).Font.Fill.ForeColor.RGB = vbWhite
.TextRange.Paragraphs(3).Font.Bold = msoTrue
.TextRange.Paragraphs(3).Font.Glow.Transparency = 1
ElseIf ComboBox7 = "" And TextBox9 <> "" Then
.TextRange = .TextRange & vbCrLf & vbCrLf & TextBox9
.TextRange.Paragraphs(3).Font.Size = 18
.TextRange.Paragraphs(3).Font.Name = "Calibri"
.TextRange.Paragraphs(3).Font.Fill.ForeColor.RGB = vbWhite
.TextRange.Paragraphs(3).Font.Bold = msoTrue
End If
End With
Next
Set dict7 = Nothing
End Sub